2
void func(){ 
     int *ptr;
     printf("\n *** %d\n",*ptr);
     ptr=malloc(sizeof(int));
     *ptr=111;
     printf("\n ##### %d\n",*ptr);
}

int main()
{
    func();
    func();
    return(0);
}

Output in GCC

 *** -1991643855   // Junk value for the first call
 ##### 111         // After allocating and assigning value
 *** 111           // second call pointer the value which 111
 ##### 111         // second call after assigning

I am confused by the behaviour of malloc in the func(). After the first call, the local variable pointer ptr is cleared in the stack frame. During the second call, ptr is created again in a new stack frame. So the ptr is not pointing to anywhere. So how come when it is printed in the second call, it points to the memory location for 111. It may be very silly. I searched with Google a lot but found no clear answer.

PaulMcKenzie
  • 34,698
  • 4
  • 24
  • 45
subby
  • 21
  • 1
  • 1
    It might be that the new frame created is on the same memory. That said, this is actually envoking undefined behaviour, so nothing can be said for sure. – Haris Aug 12 '16 at 05:20
  • You might find [this answer](http://stackoverflow.com/a/6445794/434551) useful. – R Sahu Aug 12 '16 at 05:38
  • 2
    Add `printf("Just for kicks - %d\n", 37);` between the two calls to `func()` and see how the behaviour changes. Also note that output lines should end with a newline; the newline at the beginning is optional and should really only be needed if you need double spacing. Your output may not to appear until you output a newline after it — which can be misleading, sometimes. – Jonathan Leffler Aug 12 '16 at 06:21
  • 1
    This is obviously `C` code, as `malloc` requires a cast in C++. – PaulMcKenzie Aug 12 '16 at 06:35

3 Answers3

5

ptr will have indeterminate value before it is assigned some value. And dereferencing a pointer with indeterminate value will invoke undefined behavior, which can result in anything — including printing a garbage value as seen in your first call to func, printing 111 as seen in your second call to func, or even crashing your program.

As a side note, add free(ptr); at the end of func so that your program frees memory dynamically allocated with malloc.

sps
  • 2,720
  • 2
  • 19
  • 38
1

This is actually envoking undefined behaviour, so nothing can be said for sure. Accessing variables not initialized is undefined behaviour.

That said, It might be that the new frame created is on the same memory.

Haris
  • 12,120
  • 6
  • 43
  • 70
  • 1
    It would be good to mention *what* actually invokes undefined behaviour and *why* . – alk Aug 12 '16 at 08:18
1

Declaring a pointer and dereferncing it before you initialise it is undefined behaviour - it could contain anything. I believe that the reason your pointer prints the same value the second time is because func()'s stack frame is popped after the first call. You then call it again, causing an identical stack frame to be pushed where the first one is. The memory it gave to store the pointer just happened to be the same memory that was allocated to the pointer in the previous call.

Note: You should be freeing the memory that you allocated before exiting the function, or you will have memory leaks.

naffarn
  • 526
  • 5
  • 12