?
Solved

Allocating bytes on the Stack

Posted on 2005-04-01
7
Medium Priority
?
332 Views
Last Modified: 2010-04-16
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
Comment
Question by:Tom Knowlton
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 1

Assisted Solution

by:adritab
adritab earned 400 total points
ID: 13684170
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
 
LVL 10

Expert Comment

by:NetworkArchitek
ID: 13684190
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
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 13684288
>>>Try this to print the memory address to the Debug window:

Seems to work...what about seeing the value at that location?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 5

Author Comment

by:Tom Knowlton
ID: 13684300
NetworkArchitek please tell me how to activate the Stack Viewer.
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 13684347
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
 
LVL 10

Assisted Solution

by:NetworkArchitek
NetworkArchitek earned 400 total points
ID: 13684645
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
 
LVL 8

Accepted Solution

by:
RomanPetrenko earned 1200 total points
ID: 13695944
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

Independent Software Vendors: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question