Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get stack memory address of a string in visual studio 2010 C#

I am currently doing a course at college in computer science and we recently saw how to get memory addresses of variables with & and * in the debugger. My teacher told that local value type variables are in the stack and reference type are in the heap. But he also told that the stack should contain the address where strings are in the heap.

I don't know if you understand what I'm trying to say but it should be possible because I saw it in the notes he gave to us. But when he tried to show us, it didn't work and he didn't know why.

I want to know how to do it but when I try I always get this error message:

Cannot take the address of, get the size of, or declare a pointer to a managed type ('string').

I write &nameofstringvariable without quotes in the debugger. I like to understand how things work and I searched hours on google for this problem but I never found a solution. Tried with unsafe code and it didn't work.

like image 226
handyguy88 Avatar asked Mar 06 '14 19:03

handyguy88


1 Answers

I am currently doing a course at college in computer science and we recently saw how to get memory addresses of variables with & and * in the debugger

& gives you the address of a fixed variable of unmanaged type in C#; the result is a pointer. * dereferences a pointer and turns it back into a variable, it does not give you the address of a variable.

My teacher told that local value type variables are in the stack and reference type are in the heap.

Let's make sure we get this right, because this is very confusing.

A local variable goes on the stack if its lifetime is known to be short, regardless of whether the variable contains an instance of value type or a reference to the heap. Note that a local variable's lifetime is not required to be short! Consider for example a closed-over outer variable of a lambda; its lifetime is the same as the associated delegate which is not short, and so it cannot go on the stack.

An instance of a reference type goes on the heap, and therefore the variables associated with an instance of reference type are on the heap. But the reference need not be. References and instances of reference type are as different as cakes and cookbooks.

he also told that the stack should contain the address where strings are in the heap.

Correct; if there is a short-lived variable or value which is a reference to a string on the heap then that variable or value is stored on the stack. Note that this is a managed reference, not an unmanaged reference.

But when he tried to show us, it didn't work and he didn't know why.

Perhaps he should be the one asking the question here then.

I want to know how to do it but when I try I always get this error message:

Correct. You're not allowed to manipulate managed references like that.

I write &nameofstringvariable without quotes in the debugger

That gives you the address of the variable that contains the reference. It doesn't give you the reference!

Look, you have a piece of paper that says "123 Sesame Street" on it. That's a reference to a house. You put that piece of paper in 1600 Pennsylvania Avenue. When you ask for the address of the piece of paper, it's in 1600 Pennsylvania Avenue, not 123 Sesame Street.

Regardless, it's not legal to take the address of a variable of managed type.

I like to understand how things work and I searched hours on google for this problem but I never found a solution

You should read chapter 18 of the C# specification. Carefully.

You should also read all my articles on this subject, and get your teacher to read them while you are at it.

https://ericlippert.com/2009/02/17/references-are-not-addresses/

https://ericlippert.com/2009/04/27/the-stack-is-an-implementation-detail-part-one/

https://ericlippert.com/2009/05/04/the-stack-is-an-implementation-detail-part-two/

https://ericlippert.com/2010/09/30/the-truth-about-value-types/

https://ericlippert.com/2010/10/11/debunking-another-myth-about-value-types/

https://ericlippert.com/2012/01/16/what-is-the-defining-characteristic-of-a-local-variable/

So now some questions you didn't ask:

So it is impossible to obtain the managed reference to a string as a pointer?

Yes. It is impossible in the C# language to get a managed reference as a pointer.

But I can do this!

string str = "Hello World";
fixed (char* p = str) { /*...*/ }

and now I have a pointer to the first character in the string. Isn't that the same thing?

Certainly not! That's an unmanaged pointer to a char, not a managed pointer to a string. The location of the first slice of bread in the bread box is not the same thing as the location of the bread box.

Is it possible in the debugger to get the managed address associated with a string?

Sure. Look at the stack frame in the memory window; some of those values will be managed addresses.

like image 108
Eric Lippert Avatar answered Nov 12 '22 12:11

Eric Lippert