The problem is that local pointer variables to the struct appear to have the same address, although they are defined locally within each function.
The question is why they are appearing to share a memory location (attached debugger screenshots), and how can they have their own unique memory allocation.
typedef struct Map Map;
struct Map {
const char *key;
const void *value;
Map *next;
};
void testPut() {
struct Map *map;
Test test1 = {"name1", 1};
Test test2 = {"name2", 2};
Test test3 = {"name3", 3};
}
void testGet() {
struct Map *map; //this seems to have same address as map in testPut
Test test1 = {"name1", 1};
Test test2 = {"name2", 2};
Test test3 = {"name3", 3};
}
testPut
testGet
CodePudding user response:
The pointers are located on the stack. Both functions seem to have the same stack size. If you call them one after another the first variable of the second function will probably have the same address as the first variable of the first call.
So when you jump into testPut()
the stack is increased by (i do not exactly know, but assume) 8bytes.
When the function is exited, the stack gets decreased by those 8bytes again.
And when you then access testGet()
the stack for that function is created with the same size as testPut()
. Then the address in testPut()
and testGet()
are the same.
The address should change if you try to:
- add a simple
int foo;
in front ofstruct Map *map;
intestGet()
or - as stated in the comments put a
static
in front of the struct
CodePudding user response:
When an object is defined inside a function without static
, extern
, or _Thread_local
, it has automatic storage duration. This means memory is reserved for it only until execution of its associated block ends. When the function returns, the memory may be reused for other purposes.
You have examined calls to testGet
and testPut
at different times. The memory is simply being used for one purpose at one time and a different purpose at another time.
CodePudding user response:
The local variables in testPut
and testGet
appear to have the same address in memory because they have automatic storage, hence are created on the stack upon entering each function (by code at the beginning of the function) and both functions happen to be called with the same stack depth and define the same local variables.
This is completely circumstantial, these addresses may change:
- if you call
testPut()
ortestGet()
from another place in your program - if you run the program another time on modern OSes that implement address space randomisation
- if you compile with a different compiler or just different flags.
- if you change something in the rest of these functions
- if you change something elsewhere in the program
- if anything else happens or does not happen...
It is educational to try and understand how objects and statements are implemented in memory and code, it will help grab the concept behind pointers, but remember that none of this is defined by the C Standard, ony the semantics of the abstract machine.