By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,613 Members | 1,890 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,613 IT Pros & Developers. It's quick & easy.

Memory allocation in managed code

P: 2
Here's a seemingly simple issue that has me scratching my head. In the following snippet of managed code (C#), I declare some integers without assigning them values.

Expand|Select|Wrap|Line Numbers
  1. static void Main()
  2. {
  3.   int x;
  4.   int y;
  5.   int z;
  6. }
Next I put a breakpoint on z or the closing brace and I view the address of each variable in my watch window. So, &x, &y, &z. And it looks normal. Each varialbe has an address and has been initialized to a value of 0.

Expand|Select|Wrap|Line Numbers
  1. -&x    0x03abecc4
  2.     x    0
  3. -&y    0x03abecc0
  4.     y    0
  5. -&z    0x03abecbc
  6.     z    0
Now, if I assign a values to x and y,

Expand|Select|Wrap|Line Numbers
  1. static void Main()
  2. {
  3.   int x = 0;
  4.   int y = 1;
  5.   int z;
  6. }
my watch window now shows

Expand|Select|Wrap|Line Numbers
  1. &x    Expression has no address
  2. &y    Expression has no address
  3. -&z    0x03abecbc
  4.     z    0    int
That is, x and y now supposedly have no address, but do have a value. How is that possible?

And this seems to be the case with any CLR language, not just C#.

However, if I try the same thing in unmanaged C, I get what I expect.

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.   int x = 1;
  4.   int y = 1;
  5.   int z = 1;
  6.   return 0;
  7. }
Expand|Select|Wrap|Line Numbers
  1. -&x    0x0012ff60
  2.  
  3. -&y    0x0012ff54
  4.     1
  5. -&z    0x0012ff48
  6.     -858993460
I'm using VS2005. Any ideas?
Apr 20 '07 #1
Share this Question
Share on Google+
1 Reply


P: 2
Sorry, for those last two code blocks, I meant to post:

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.   int x = 0;
  4.   int y = 1;
  5.   int z;
  6.   return 0;
  7. }
Expand|Select|Wrap|Line Numbers
  1. -&x    0x0012ff60
  2.  
  3. -&y    0x0012ff54
  4.     1
  5. -&z    0x0012ff48
  6.     -858993460
Apr 20 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.