views:

97

answers:

2

Hi,

Doing this in a console program:

object x = new string(new char[0]);

If one set a breakpoint rigt after x's assignment and bring up the quick watch window, what is the debugger displaying for &x? I was expecting x's address but it looks like I'm wrong?


EDIT: I'm asking this because of another thread, which was pointing out that if one do

object x = new string(new char[0]);
object y = new string(new char[0]);

x and y will reference the same object. As I wanted to check it out, I tried inspecting these variables' addresses in the debugger using the & operator and, at first, I had the impression they were different. But, after expanding the result, second level values do match - so these seem to be "real addresses".

+2  A: 

If I display x I get "" with the type reported as "object {string}" - which is what I expect.

&x displays 0x062fd7f0 (in my case) with the type reported as "object&*". Expanding that gives another pointer and the type as "object&".

In the first case the debugger has enough information to work out the type and show you something useful.

In the second case all the debugger "knows" is that the variable is an object so can only display pointers (I think).

UPDATE

Your update makes sense. The value reported by &x and &y are the pointers to the variables. The value they hold is the pointer to the immutable string "".

ChrisF
Sorin Comanescu
Sorin Comanescu
A: 

You cant do this, because memory management is handled by CLR

Tror
Yes, edited my question to clarify (hopefully) what I was trying to do.
Sorin Comanescu