In the following code, the explanation for the failure to print anything is that the pointer returned by get_message() is out of scope:
char *get_message() {
char msg [] = "Aren’t pointers fun?";
return msg ;
}
int main (void) {
char *foo = get_message();
puts(foo);
return 0;
}
When run in gdb, it turns out that the data at the position of foo is the string "Aren't pointers fun?":
Old value = 0x0
New value = 0x7fffffffde60 "Aren’t pointers fun?"
(This seems consistent with answers which states that the data for a pointer which passes out of scope remains in memory), but the documentation for "puts" states first data is copied from the address given: presumably 0x7fffffffde60 in this case.
Therefore: why is nothing output?
EDIT: Thanks for your answers: I ran the original code to completion in gdb, the call to puts does indeed change the data at the address where foo was stored.
(gdb) p foo
$1 = 0x7fffffffde60 "Aren’t pointers fun?"
(gdb) n
11 return 0;
(gdb) p foo
$2 = 0x7fffffffde60 "`\336\377\377\377\177"
Interestingly, the code did print the message when I changed the code for change_msg() to:
char *get_message() {
char *msg = "Aren’t pointers fun?";
return msg ;
}
In this case, the data at foo (address 0x4005f4 - does the smaller size of the address mean anything?) remains the same throughout the code. It'd be cool to find out why this changes the behaviour
The variable msg is allocated on the stack of get_message()
char msg [] = "Aren’t pointers fun?";
Once get_message() returns, the stack for that method is torn down. There is no guarantee at that point of what is in the memory that the pointer returned to foo
now points to.
When puts() is called, the stack is likely modified, overwriting "Aren't pointer's fun."