Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning local data from functions in C and C++ via pointer

I have argument with my friend. He says that I can return a pointer to local data from a function. This is not what I have learned but I can't find a counterargument for him to prove my knowledge.

Here is illustrated case:

char *name() {
    char n[10] = "bodacydo!";
    return n;
}

And it's used as:

int main() {
    char *n = name();
    printf("%s\n", n);
}

He says this is perfectly OK because after a program calls name, it returns a pointer to n, and right after that it just prints it. Nothing else happens in the program meanwhile, because it's single threaded and execution is serial.

I can't find a counter-argument. I would never write code like that, but he's stubborn and says this is completely ok. If I was his boss, I would fire him for being a stubborn idiot, but I can't find a counter argument.

Another example:

int *number() {
    int n = 5;
    return &n;
}

int main() {
    int *a = number();
    int b = 9;
    int c = *a * b;
    printf("%d\n", c);
}

I will send him this link after I get some good answers, so he at least learns something.

like image 882
bodacydo Avatar asked Jun 27 '10 13:06

bodacydo


People also ask

Can you return a local variable from a function in C?

We can pass pointers to the function as well as return pointer from a function. But it is not recommended to return the address of a local variable outside the function as it goes out of scope after function returns.

What is the output of C program with functions and pointers?

17) What is the output of C Program with functions and pointers.? Explanation: It is called Passing a variable by reference.

How do I return a pointer to a local variable?

So to execute the concept of returning a pointer from function in C/C++ you must define the local variable as a static variable. Example: In the below program, the line of code(int lv = n1 * n1;) will give warning as it is local to the function.

Can a pointer be used as a return type for a function?

Return Function Pointer From Function: To return a function pointer from a function, the return type of function should be a pointer to another function. But the compiler doesn't accept such a return type for a function, so we need to define a type that represents that particular function pointer.


4 Answers

Your friend is wrong.

name is returning a pointer to the call stack. Once you invoke printf, there's no telling how that stack will be overwritten before the data at the pointer is accessed. It may work on his compiler and machine, but it won't work on all of them.

Your friend claims that after name returns, "nothing happens except printing it". printf is itself another function call, with who knows how much complexity inside it. A great deal is happening before the data is printed.

Also, code is never finished, it will be amended and added to. Code the "does nothing" now will do something once it's changed, and your closely-reasoned trick will fall apart.

Returning a pointer to local data is a recipe for disaster.

like image 169
Ned Batchelder Avatar answered Sep 29 '22 13:09

Ned Batchelder


you will get a problem, when you call another function between name() and printf(), which itself uses the stack

char *fun(char *what) {
   char res[10];
   strncpy(res, what, 9);
   return res;
}

main() {
  char *r1 = fun("bla");
  char *r2 = fun("blubber");
  printf("'%s' is bla and '%s' is blubber", r1, r2);
}
like image 40
Peter Miehle Avatar answered Sep 29 '22 12:09

Peter Miehle


As soon as the scope of the function ends i.e after the closing brace } of function, memory allocated(on stack) for all the local variables will be left. So, returning pointer to some memory which is no longer valid invokes undefined behavior. Also you can say that local variable lifetime is ended when the function finished execution.

Also more details you can read HERE.

like image 40
Incognito Avatar answered Sep 29 '22 11:09

Incognito


My counter-arguments would be:

  • it's never OK to write code with undefined behavior,
  • how long before somebody else uses that function in different context,
  • the language provides facilities to do the same thing legally (and possibly more efficiently)
like image 44
Nikolai Fetissov Avatar answered Sep 29 '22 13:09

Nikolai Fetissov