-2

I am studying the working of an automatic variable. I know that it is only accessible inside the block or function in which it is declared and its lifetime is within that same function or block. So the following piece of code I am trying to check.

/Declaration of header files/

void testfn(void);
int   *p;
int main(void)
{
  testfn();
  print("%d\n",*p);
  return 0;
}

void testfn(void)
{
  int x=444;
  p=&x;
}

The output is - 444

I am wondering that when the testfn(); exits,the variable x will be destroyed. Then how in main function the pointer (*p) prints 444. How this works...or if I am missing something. Please clarify my doubt.

Thanks

Magisch
  • 7,312
  • 9
  • 36
  • 52
amit kr
  • 117
  • 1
  • 1
  • 7
  • undefined behavior... – dandan78 Apr 07 '16 at 08:08
  • That's the thing with undefined behaviour: It can look like the expected behaviour when in fact it isn't. It's undefined, not defined wrong behaviour, unfortunately. – M Oehm Apr 07 '16 at 08:11
  • 1
    The address has the same value it had when the function was active, but the memory it points to is no longer allocated to that function. But this is all a real waste of time. I know you're trying to learn, but this isn't something you should be posting here. – Tom Karzes Apr 07 '16 at 08:12

4 Answers4

1

The memory location that was previously reserved for variable x is not overwritten yet. But it may be at any time. That's why your code leads to undefined behaviour.

In the following example, the memory location that was previously reserved for variable x will be overwritten by the value that is assigned to the variable y. Since the pointer p still points to that location, *p will evaluate to this new value:

#include <stdio.h>

int *p;

void test1(void) {
    int x = 444;
    p = &x;
}

void test2() {
    int y = 15;
}

int main(void) {
    test1();
    test2();
    printf("%d\n",*p);
    return 0;
}
tonisuter
  • 789
  • 5
  • 13
1

It is coincidence that the original value still remains. With another compiler, or with another compilation configuration, it could take any other value or the program could just crash.

If between the testfn and printf functions you call any other function that does something with its local variables, you might see that the 444 value is not obtained anymore. But this is just, again, coincidence.

atturri
  • 1,133
  • 7
  • 13
1

p points to the stack where x was stored. If the memory location hasn't been used for something else you will most likely get 444.

Try to insert another function call before printing p and see what happens:

#include <stdio.h>
#include <math.h>

void foo() {
  int y=123;
}

void testfn(void);
int   *p;
int main(void)
{
  testfn();
  foo();
  printf("%d\n",*p);
  return 0;
}

void testfn(void)
{
  int x=444;
  p=&x;
}

On my machine, the output is now:

123

Since the code results in undefined behaviour, the result could be different if I try this on another platform. But you can see that undefined behaviour can lead to strange bugs.

Klas Lindbäck
  • 33,105
  • 5
  • 57
  • 82
1

The fact that the value still remains is completly coincidental (not guaranteed) because nothing has overwritten it yet.

You can not count on this, it may fail, may print garbage or may crash your program or pc even.

Don't use this, it is considered undefined behavior.

Magisch
  • 7,312
  • 9
  • 36
  • 52