2

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

I have a question, first of all look at the code

    #include <stdio.h>

int sum();          /* function declaration */

int main()
{
    int *p2;
    p2 = sum();         /* Calling function sum and coping its return type to pointer variable p2  */
    printf("%d",*p2);
} /*  END of main  */ `

int sum()           
{
    int a = 10;
    int *p = &a;
    return p;
} /*  END of sum */

I think the answer is 10 and address of variable a, but my tesacher says that a is local to the function come so a and its value will be deleted from the memory location when the function returns or is finished executing. I tried this code and the answer is weel of course 10 and address of a, I use the GNU/GCC compiler. Can anyone say what is right and wrong. Thanks in advance.

Community
  • 1
  • 1
john
  • 53
  • 1
  • 6
  • 2
    The program as given wont compile. Shouldn't the return type of `sum` be `int*`? – pmr Aug 18 '12 at 01:28
  • putting asides the whys of it, if you just want verification that your teacher is right, run your program under valgrind (assuming you are in a linux or cygwin environmen). say your program is called a.out, you run valgrind a.out. Valgrind will inform you that you have committed an error. – frankc Aug 18 '12 at 01:48
  • at pmr- this program compiles and runs produces the ouput i,e 10 on a few machines, on my computer, i run linux on my cousins system he runs windows and uses an ide. – john Aug 19 '12 at 17:08

5 Answers5

3

Your teacher is absolutely right: even if you fix your program to return int* in place of int, your program still contains undefined behavior. The issue is that the memory in which a used to be placed is ripe for reuse once sum returns. The memory may stay around untouched for you to access, so you might even print ten, but this behavior is still undefined: it may run on one platform and crash on ten others.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
2

You may get the right result but that is just because you are lucky, by the time the sum() is returned, the memory of a is returned to the system, and it can be used by any other variables, so the value may be changed.

For example:

#include <stdio.h>

int* sum();          /* function declaration */
int* sum2();          /* function declaration */

int main()
{
    int *p2;
    p2 = sum();         /* Calling function sum and coping its return type to pointer variable p2  */
    sum2();
    printf("%d",*p2);
}

int* sum()           
{
    int a = 10;
    int *p = &a;
    return p;
} /*  END of sum */

int* sum2()           
{
    int a = 100;
    int *p = &a;
    return p;
} /*  END of sum */

With this code, the a will be reused by sum2() thus override the memory value with 100.

Here you just return a pointer to int, suppose you are returning an object:

TestClass* sum()           
{
    TestClass tc;
    TestClass *p = &tc;
    return p;
}

Then when you dereference tc, weird things would happen because the memory it points to might be totally screwed.

Baiyan Huang
  • 6,463
  • 8
  • 45
  • 72
  • ok!! So she is right, thank you if i make var a as static then what might happen? – john Aug 18 '12 at 01:24
  • static variable have global lifetime after it is first accessed, so it should be ok – Baiyan Huang Aug 18 '12 at 01:28
  • For certain values of "ok"; making `a` static makes the function non-reentrant. In this case, the *right* answer is to return the *value* of `a`, not its address. – John Bode Aug 18 '12 at 12:53
0

Your pointer still points to a place in memory where your 10 resides. However, from Cs point of view that memory is unallocated and could be reused. Placing more items on the stack, or allocating memory could cause that part of memory to be reused.

user1518845
  • 165
  • 1
  • 2
  • 13
0

The object a in the function sum has automatic lifetime. It's lifetime will be ended as soon as the scope in which it has been declared in (the function body) is left by the program flow (the return statement at the end of the function).

After that, accessing the memory at which a lived will either do what you expect, summon dragons or set your computer on fire. This is called undefined behavior in C. The C standard itself, however, says nothing about deleting something from memory as it has no concept of memory.

pmr
  • 58,701
  • 10
  • 113
  • 156
0

Logically speaking, a no longer exists once sum exits; its lifetime is limited to the scope of the function. Physically speaking, the memory that a occupied is still there and still contains the bit pattern for the value 10, but that memory is now available for something else to use, and may be overwritten before you can use it in main. Your output may be 10, or it may be garbage.

Attempting to access the value of a variable outside that variable's lifetime leads to undefined behavior, meaning the compiler is free to handle the situation any way it wants to. It doesn't have to warn you that you're doing anything hinky, it doesn't have to work the way you expect it to, it doesn't have to work at all.

John Bode
  • 119,563
  • 19
  • 122
  • 198