I have a pointer available with me to a C/C++ variable. Is it possible to exactly make out which segment of the memory this variable belongs to ? If yes, how ?
Note: I just have the address of this variable, no further information if the variable is local/global etc.
All it does is extend the data segment, which begins at a some address and grows upwards. All allocations will be greater than or equal to this starting address. On the other hand, the stack starts at the "top" of memory and grows downwards towards the current end of the heap.
The address of a local variable isn't stored anywhere, unless you have a pointer pointing to the local variable. The local variables themselves are typically stored on the stack on most modern systems.
Heap memory is used by all the parts of the application whereas stack memory is used only by one thread of execution. Whenever an object is created, it's always stored in the Heap space and stack memory contains the reference to it.
A stack frame is identified by a memory address in a register called the base-pointer, EBP (this is specific to IA-32 architecture, but other CPU's might have a similar register). The EBP contains the base of the current stack frame, that is, the highest numbered address in the stack frame.
Find out whether your architecture has pointers to your heap or stack region. Usually there are some stackpointers or framepointers..
Then compare your actual address to those addresses and decide where they belong.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With