0

Possible Duplicate:
Can a local variable's memory be accessed outside its scope?

I'm trying to understand why I get this output for the below program

[hello] [0xbfde68f4]
[world] [0xbfde68f4]
[world] [0xbfde68f4]

The program is

int main(void)
{
    char **ptr1 = NULL;
    char **ptr2 = NULL;

    ptr1 = func1();
    ptr2 = func2();
    printf(" [%s] [%p]\n",*ptr1, (void*)ptr1);

    printf(" [%s] [%p]\n",*ptr2, (void*)ptr2);

    printf(" [%s] [%p]\n",*ptr1, (void*)ptr1);

    return 0;
}

char** func1()
{
    char *p = "hello";
    return &p;
}

char** func2()
{
    char *p = "world";
    return &p;
}

I understand that it's not a good practice to return address of local variables but this is just an experiment.

Community
  • 1
  • 1
John Eipe
  • 10,922
  • 24
  • 72
  • 114
  • why are the memory addresses same? and why does ptr1 lose the value? – John Eipe Aug 09 '12 at 04:26
  • There's no special reason they're the same. They just happen to be. It's just the luck of the draw. As for why `ptr1` loses the value, it's because the system re-used that memory for some other purpose once it was freed. – David Schwartz Aug 09 '12 at 04:26
  • so that means it could retain the value also. It just happened this way this time? – John Eipe Aug 09 '12 at 04:28
  • Add a variable to the stack in one of the functions but not the other before p; you'll then get different addresses, and maybe the whole "stack" thing will be a little clearer. – tbert Aug 09 '12 at 04:37
  • though similar questions had been posted before...sorry i had to ask it once again. but it helped me. thank you all for your answers – John Eipe Aug 09 '12 at 04:38

3 Answers3

1

The memory address is re-used. First it holds the address of the constant holding "hello", then it is re-used to hold the address of the constant holding "world".

Once memory is no longer in use, it is available for re-use. It's generally most efficient to re-use the most recently used memory, so that's what compilers and memory managers typically try to do.

Note that it's definitely not guaranteed. You may find that this program crashes or gives different addresses on different compilers or platforms. However, re-use is very, very likely in this particular scenario, since both variables are local and assigned on the stack and no intervening code uses any stack space. If you add an intervening use of stack space, you will get different behavior.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
0

Local variables live on the stack. When you called func1(), the local variable was created, then destroyed as it went out of scope. func2() used the same space for its local variable, since func1() wasn't using it anymore.

noisecapella
  • 814
  • 7
  • 17
0

ptr1 = func1(); use the memory block as below.

prt1
 ↓
[h][e][l][l][o][\0]
 ↑
0xbfde68f4

ptr2 = func2(); use the same memory block because after exiting func1, this memory block can be reused.

     prt2
      ↓
prt1→[w][o][r][l][d][\0]
      ↑
    0xbfde68f4
xdazz
  • 158,678
  • 38
  • 247
  • 274