the values of pointers of a struct change after each call to printf.
so i have the following structs :
struct block_meta_data
{
size_t size;
struct block_meta_data * next;
int isFree;
};
typedef struct block_meta_data block_meta_data;
struct list
{
block_meta_data* base;
block_meta_data* end;
};
typedef struct list list;
i defined a global varibale of type list called blocks_list:
list blocks_list = {NULL, NULL};
and the main function looks like :
int main(void)
{
char a[] = "hello";
printf("%s\n", a);
return 0;
}
now, using a debugger i observe the values of both field of the 'blocks_list' variable before the call to printf and they both have the value NULL:
after the call to printf, the values of both fields change to :
i tried printing the values of both end and base (without using a compiler) and the issue still remains :
list blocks_list = {NULL, NULL};
int main(void)
{
char a[] = "hello";
printf("%s\n", a);
printf("base = %p | end = %p\n", blocks_list.base,blocks_list.end);
return 0;
}
output :
hello
base = 0x555eed65d000 | end = 0x555eed65d000
Can someone explain why this is happening?
CodePudding user response:
Can someone explain why this is happening?
There are a few possible explanations:
- you are not telling us the whole story (provide MCVE) -- this is the most likely one
- this is a debugging artifact -- your debugger is confused and shows something that isn't actually happening
- the compiler detected that
blocks_list
is not actually used, and didn't allocate any storage for it (usually the compiler would also set location ofblocks_list
to0
, which would prevent debugger from displaying it).
Since you appear to be using Linux, note that you can tell where the value is overwritten using GDB watch points:
(gdb) watch -l blocks_list.base
(gdb) continue
# GDB will stop when blocks_list.base changes its value