-1
#include <stdio.h>

int* function1(void);

int main()
{

    int x = 10;
    int *p = function1();

    printf("%d\n", *p);
    printf("%d\n", p);
}

int* function1(void)
{
    int z;
    z = 20;
    z++;
    return &z;
}
  • Variable 'z' is local to the 'function1', and is not alive after the 'function1' is terminated.
  • Now to access the value at the memory space of the variable 'z', its address is returned by the function.
  • So, even after the termination, will the memory space of the variable 'z' will still be reserved, as the pointer accesses the variable?, in such case what will be the properties of the memory space?
  • Or What if some-other variable is allocated with the same memory space of variable 'z'?

Note: GCC compiler of code blocks has compiled the program successfully, without any error and warning.

Swordfish
  • 12,971
  • 3
  • 21
  • 43

3 Answers3

1

The variable z does no longer exist after the function function1 finishes it's execution. In function main you are trying to reference a memory address which has been deallocated after the function's call. This will cause undefined behavior.

haccks
  • 104,019
  • 25
  • 176
  • 264
  • To be picky: Returning the pointer value alone is not undefined behaviour. – Swordfish Sep 26 '18 at 10:52
  • @Swordfish; I didn't get you. Can you please elaborate? – haccks Sep 26 '18 at 10:52
  • 2
    `int* bar(void) { int i; return &i; } /* ... */ int *foo = bar();` is not undefined. Dereferencing `foo` is. – Swordfish Sep 26 '18 at 10:54
  • @Swordfish; Ok. Got you know. – haccks Sep 26 '18 at 10:58
  • 1
    @Swordfish: Per C 2018 6.2.4 2: “The value of a pointer becomes indeterminate when the object it points to (or just past) reaches the end of its lifetime.” Per 3.19.2 1, an indeterminate value may be a trap representation, so using a pointer (even without dereferencing it) after the object it pointed to no longer exists may cause a trap. – Eric Postpischil Sep 26 '18 at 11:05
  • @EricPostpischil I didn't say anything else, did I? – Swordfish Sep 26 '18 at 11:07
  • @Swordfish: `int *foo = bar();` uses the value of the pointer returned by `bar` after the lifetime of the pointed-to object ends and therefore may trap. – Eric Postpischil Sep 26 '18 at 11:10
1

In general what you do is undefined.

However, on Intel architectures z is on the stack and after return, if you don't call any other function the value will probably still be available because the memory has not yet been reused. As soon as you call another function, the memory will probably be overwritten and so will contain garbage for you.

In general: Don't do this!

Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41
0

When the function call happens, all your local variables will be in stack. During function call, the stack variables can be modified. When the function call returns, the stack pointer is decremented

Hence, you will be accessing something which is not guaranteed in any way. In programming languages, this is addressed as a case of undefined behaviour, since you are overriding the rules of programming language.

In this case of function, given that you stack frame is still active and not modified by any other code, you might get the same value that you wrote to that address.

But is not guaranteed in anyway and dont assume anything not guaranteed.

user0x0
  • 253
  • 1
  • 7