1

Recently I read a thread on stackoverflow that returning a local variable must be avoided whether its a pointer type or a normal variable. I saw an example in my C book and it was returning a local variable, so I thought to try it again

#include <stdio.h>
int func(int a, int b)
{
    int d, e, f;
    d = a;
    e = b;
    f = d+e;
    return f;
}
int main(void)
{
    int c = func(1, 2);
    printf("hello\n");//I put this printf in between
    printf("c = %d\n", c);//here c should be overwritten
    return 0;
}

In that thread it was said, that if I put anything between function call and accessing that variable, I will miss the value.

I am able to access the local variable whatever I do, yet I recall I wrote an example according to that thread and was showing same behaviour as told.

What am I missing?

acidlategamer
  • 163
  • 2
  • 14
  • Is this the exact same code as the book said? Otherwise, it is normal behaviour since you already use 'c' to save the return value of the function in main. You can always read its value after assign value to it. – Kit Fung Apr 11 '17 at 16:00
  • 1
    Returning a local variable's *value* is fine... returning a *pointer to* a local variable is not (unless it's static and you know the consequences). – Dmitri Apr 11 '17 at 16:00
  • Related question: http://stackoverflow.com/questions/4824342/returning-a-local-variable-from-function-in-c – msc Apr 11 '17 at 16:00
  • I thought that the moment a local variable returns, it is destroyed, so it will be undefined behaviour. – acidlategamer Apr 11 '17 at 16:03
  • 1
    When you return a local variable's value, you effectively copy the value from the local variable to wherever the return value is assigned. So you can't use a pointer to the local variable itself once the function returns, but you *can* use it's value (at the time of the return), copied to another variable via the return. – Dmitri Apr 11 '17 at 16:05
  • The case where you run into a problem is when you try to return an *array*, because that implicitly converts to a pointer to the array. – Barmar Apr 11 '17 at 16:11
  • You don't return a local variable, but the value it holds at that moment! – too honest for this site Apr 11 '17 at 16:16
  • @Dmitri: Returning a local variable **is** bad, resp. UB! – too honest for this site Apr 11 '17 at 16:16

1 Answers1

5

The unnamed thread you mentioned has mislead you.

In your example, func() is returning an integer. Returning an integer is always safe, regardless of where it came from.

Values (int, char, double) are never shared. Each time you pass/return them, a copy of that value is passed/returned, so it is safe.

Pointers (int*, char*, double*) can share the memory location they are pointing at. Passing/returning them can be dangerous, because the value at that memory location will change over time. You have to be careful with pointers.

  • I guess something is missing from the explanations then. a local variable can be copied but not a local pointer. why? is this about segmentation in process, that one region's stack can't be accessed by another region in stack. so this local pointer value is returned, but at that address in caller's function's stack, garbage or 0 will be set. – acidlategamer Apr 11 '17 at 16:33
  • in case of doing a malloc and then returning a local pointer is accesible just because heap is common to all functions in a program. – acidlategamer Apr 11 '17 at 16:36
  • 1
    It's not really about whether one function can access another's stack space -- it's just that the variable's lifetime is over when the function returns (unless it's static), and the region it occupied may become invalid or be reused for something else after the function returns. It is fine, for example, to pass a pointer to a local variable *to* a function, because the function call in which the pointer would be used occurs during the lifetime of the variable it points to and not after it ends. – Dmitri Apr 11 '17 at 17:05