3

Possible Duplicate:
Returning the address of local or temporary variable
Can a local variable's memory be accessed outside its scope?

I know that I should not return pointers to local function variables (local stack variables) because when the function return, the variables will be invalid and the stack will be cleaned up unless I made these variables static or allocated them on heap.

The following code demonstrates that:

const char* v1() {
   return "ABC";
}

const char* v2() {
    string s = "DEF";
    return s.c_str();
}

const char* v3() {
    static string s = "JHI";
    return s.c_str();
}

cout << v1() << endl; // Output: ABC
cout << v2() << endl; // Output: garbage (♀   ╠╠╠╠╠╠╠╠)
cout << v3() << endl; // Output: JHI

However, I returned pointer to a primitive local function variable and I was able to get its value although it is not static, as the following code shows:

int i1() {
    int i = 5;
    return i;
}

int* i2() {
    int i = 6;
    return &i;
}

int* i3() {
    static int i = 7;
    return &i;
}

cout << i1() << endl;  // Output: 5
cout << *i2() << endl; // Output: 6 !!
cout << *i3() << endl; // Output: 7

The compiler only gives me warning that I am returning address of local variable or temporary (Visual C++ 2008). Is this behaviour common to all compilers and how the compiler allows me to use pointer to a local function variable to access the value it points to although the variable is invalidated when the function return?

Community
  • 1
  • 1
Nizar
  • 416
  • 3
  • 13
  • 3
    Its value happens to remain in memory at the same location. The fact that this works is either an accident, or a result of your compiling in "Debug" mode. I'm not sure what the question is—you already know you shouldn't be doing this. Listen to the warnings your compiler emits! – Cody Gray - on strike Jan 03 '12 at 06:42

2 Answers2

3

you return an address. Returning an address is valid - always. But in your case, you also dereference it. This is undefined behavior. Theoretically, for undefined behavior, anything can happen. The compiler is even allowed to embed code to format your hard-disc. Practically it will dereference the address without any checks. If it is still accessible, it'll return the value at that address otherwise it'll cause an access violation.

Your address is on the stack, so it is always accessible. Depending on the calls you made in between, the value might still be there or not. So in simple cases, you get the value back, in more complicated cases you won't. It may even work sometimes and sometimes it does not.

For more information, you should read some information on how function calls are made in assembler to understand what the compiler is doing there on the stack (placing parameters, return address, placing local variables, stack cleanup on return, calling conventions).

Tobias Langner
  • 10,634
  • 6
  • 46
  • 76
2

it can be removed from the stack because it is local but the value will remain until another overwirte it. it is c++ unsafe language u can do much strange things

Hussein Zawawi
  • 2,907
  • 2
  • 26
  • 44