1

The following case makes more confuse to me. As far as I know, local variables don't return by pointers or reference. for example

char * foo()
{
  return "Hello world";
}

int*  fooo() {
    static int i = 100;
    return &i;
}

What would happen in both cases ?

Srijeyanthan
  • 126
  • 1
  • 2
  • 1
    The first shouldn't compile cleanly in modern C++. – chris Apr 24 '13 at 03:28
  • Which means , returning like this is a standard method ? – Srijeyanthan Apr 24 '13 at 03:40
  • 2
    I wouldn't say it's "standard". It's legal, but it's not common convention. Can you give us more information about what you're trying to accomplish and why you think you need to write this code? – Cody Gray - on strike Apr 24 '13 at 03:40
  • Thank you very much , the basic reason is , returning local variable by pointer or reference is not valid after the function execution. So here , we are returning "hello world" which local to that function, so my concern here is , once function goes to out of scope , is that location valid ? – Srijeyanthan Apr 24 '13 at 03:54
  • 1
    But there are better ways to accomplish this. For example, a constant value like `"Hello world"` can just be declared as a constant in the same scope as your function: `const char* foo = "Hello world";` You don't need the function at all. – Cody Gray - on strike Apr 24 '13 at 03:56
  • Both work, but your'e setting up a trap for future maintainers – AK_ Apr 24 '13 at 04:24

3 Answers3

5

String literals are stored statically and of course the static int i is static too. You can return pointers to static variables from functions because they are not local variables, and they are not destroyed when you exit the function as stack allocated variables do. On the other hand, your first example should return a const char *,

From the C++ standard section lex.string:

A string literal ... has type "array of n const char" and static storage duration (basic.stc), where n is the size of the string as defined below, and is initialized with the given characters...

perreal
  • 94,503
  • 21
  • 155
  • 181
  • The answer also mentions it, "course the `static int i` is static too". You can return pointers to all static variables safely. – perreal Apr 24 '13 at 03:58
0

The first code will not compile. You can cast the string to char array and return back. The second code will compile, but your variable will go out of scope. So the reference you will have when the function returns will not be valid anymore. It is bad to return a local variable by address. Is there any specific reason for wanting this behavior?

You can new/malloc a char array or int, and then return it back. You are guaranteed to have the variable in scope, as long as you do not manually free/delete the memory. Then you can happily access the memory from other functions.

Hope this helps.

Bill
  • 5,263
  • 6
  • 35
  • 50
  • 3
    The variable in the second case is `static`. – jogojapan Apr 24 '13 at 03:36
  • I did not see that...static variables are not local to the function, so you still have valid memory for static variables. But, there should not be any need to return address of static variables from any function, as they are accessible in the entire class in which it has been defined. – Bill Apr 24 '13 at 03:38
  • 3
    You're confusing `static` at class scope with `static` at function scope. – Cody Gray - on strike Apr 24 '13 at 03:39
  • yes, sorry about that. Yes, static variables inside a function have lifetime of the entire program. But, I think it is a bad practice to return static function level variables. Thanks for pointing it out. – Bill Apr 24 '13 at 03:42
0

your first function is not valid, you should return

const char*

and yes, you can return adresses of static variables: they are not destroyed when function returns because they are alocalted in static data memory segment