Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning reference to static local variable in C++

Tags:

c++

This question is just for my better understanding of static variables in C++.

I thought I could return a reference to a local variable in C++ if it was declared static since the variable should live-on after the function returns. Why doesn't this work?

#include <stdio.h>
char* illegal()
{
  char * word = "hello" ;
  return word ;
}

char* alsoNotLegal()
{
  static char * word = "why am I not legal?" ;
  return word ;
}


int main()
{
  // I know this is illegal
  //char * ill = illegal();
  //ill[ 0 ] = '5' ;
  //puts( ill ) ;

  // but why is this? I thought the static variable should "live on" forever -
  char * leg = alsoNotLegal() ;
  leg[ 0 ] = '5' ;
  puts( leg ) ;
}
like image 793
bobobobo Avatar asked Jun 06 '09 15:06

bobobobo


People also ask

Can we return static variable in C?

It is important to note that a static variable must be initialized by a constant literal; we cannot use a function's return value to initialize a static variable. In Example3. c, we try to initialize a static variable by using the return value of fun1().

Can you return local variables in C?

Yes you are returning an array, which is actually a pointer behind the scenes, to the address of the memory location where the contents of the variable you've initialised is stored.

Can you return a reference in C?

Functions in C++ can return a reference as it's returns a pointer. When function returns a reference it means it returns a implicit pointer.

Is it good idea to return an address or a reference of a local variable?

The return statement should not return a pointer that has the address of a local variable ( sum ) because, as soon as the function exits, all local variables are destroyed and your pointer will be pointing to someplace in the memory that you no longer own.


2 Answers

The two functions are not itself illegal. First, you in both case return a copy of a pointer, which points to an object having static storage duration: The string literal will live, during the whole program duration.

But your main function is all about undefined behavior. You are not allowed to write into a string literal's memory :) What your main function does can be cut down to equivalent behavior

"hello"[0] = '5';
"why am I not legal?"[0] = '5';

Both are undefined behavior and on some platforms crash (good!).

Edit: Note that string literals have a const type in C++ (not so in C): char const[N]. Your assignment to a pointer to a non-const character triggers a deprecated conversion (which a good implementation will warn about, anyway). Because the above writings to that const array won't trigger that conversion, the code will mis-compile. Really, your code is doing this

((char*)"hello")[0] = '5';
((char*)"why am I not legal?")[0] = '5';

Read C++ strings: [] vs *

like image 178
Johannes Schaub - litb Avatar answered Sep 27 '22 18:09

Johannes Schaub - litb


Only the pointer is static and it points to a constant string. Doing leg[ 0 ] = '5' is not ok as it modifies the constant string.

static make little difference in this case, this is really the same:

char* alsoNotLegal()
{
     return "why am I not legal?";
}
like image 36
Joakim Elofsson Avatar answered Sep 27 '22 17:09

Joakim Elofsson