• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 341
  • Last Modified:

Allocating bytes on the Stack

Declaring the following:

int i=4;

Allocates 4 bytes on the Stack...and assigns the value 4 to that location.

===================================


My question is....is there some sort of memory VIEWER I can use to see this happening?

Is there any way to LOOK AT the 4 bytes that have been allocated.....and to also verify that the value of 4 is stored there?


Thanks,

Tom
0
Tom Knowlton
Asked:
Tom Knowlton
3 Solutions
 
adritabCommented:
Use the Marshal class in the System.Runtime.InteropServices Namespace

Try this to print the memory address to the Debug window:

using System.Runtime.InteropServices;
using System.Diagnostics;

class Class1
      {
            static void Main(string[] args)
            {
                  IntPtr ip = new IntPtr();
                  int number = 123;
                        //allocate 4 bytes of memory for the integer
                  ip = Marshal.AllocHGlobal(4);
                       //write the integer at that memory location
                  Marshal.WriteInt32(ip,number);
                  Debug.WriteLine(ip.ToString());
            }
      }

There's more you can do with this class. Check the <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemruntimeinteropservicesmarshalclasstopic.asp">MSDN help</a> for more information.
0
 
NetworkArchitekCommented:
Well you can use the built in "stack viewer" in the VS debugger. Just run the debugger and set breakpoints and all and you can do this.
0
 
Tom KnowltonWeb developerAuthor Commented:
>>>Try this to print the memory address to the Debug window:

Seems to work...what about seeing the value at that location?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Tom KnowltonWeb developerAuthor Commented:
NetworkArchitek please tell me how to activate the Stack Viewer.
0
 
Tom KnowltonWeb developerAuthor Commented:
UPDATE:


I added this line to the end of Main( ):

Debug.WriteLine(Marshal.ReadInt32(ip).ToString());

and it wrote 123 to the screen.


I think that must be it!!!!

===========================================


using System;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace ConsoleApplicationCANDELETE
{
      class Class1
      {
            static void Main(string[] args)
            {
                  IntPtr ip = new IntPtr();
                  int number = 123;
                  //allocate 4 bytes of memory for the integer
                  ip = Marshal.AllocHGlobal(4);
                  //write the integer at that memory location
                  Marshal.WriteInt32(ip,number);
                  Debug.WriteLine(ip.ToString());
                  Debug.WriteLine(Marshal.ReadInt32(ip).ToString());
            
            }
      }
}
0
 
NetworkArchitekCommented:
Well, it is just using the debugger. Knowlton gave you a great programmtic way to figure it out. But if you ever do any C programming all that is doing is basically what you can do in about two or three lines of code in C. =)

Ok, so you want to set breakpoints in your code, that is basically clicking on the "gray margin" on the lines of your code. So there is a lot to debugging but if you put a breakpoint on a line right after the line of code which executes (that you want to see the value of) then run your app in the debuger "Debug -> Start" and look in the bottom and you see the Call Stack. There you will see your variables and the value of those variables as well as a plethora of other information. Just play with it and I guess any article or chapter in a book on debugging would make it all very clear ...
0
 
RomanPetrenkoCommented:
You can use WinDbg from microsoft with SOS extension to see how managed/unmanaged stack behaves.

Also you can use stackalloc keyword to allocate unmanaged types in heap.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now