0

In the code below, i expect tmp character array to be destroyed after f() returns and hence x should not be printed at all.

However, in the below function x get printed in main() but the for loop does not print the correct thing. Could someone explain this behavior. Here is the output.

abcdefg a b c d e f g abcdefg ?

k Y i

#include <iostream>
using namespace std; 

char* x;  
void f() 
{
    char tmp[100]= "abcdefg";   
    x = tmp; 

    cout << x << endl; 
    for(int i=0; i < 7; i++) 
        cout << x[i] << endl; 
}

int main() 
{
    f();

    cout << x << endl; 
    for(int i=0; i < 7; i++) 
        cout << x[i] << endl; 
}
Alexey Frunze
  • 61,140
  • 12
  • 83
  • 180
Pungs
  • 2,432
  • 1
  • 16
  • 14
  • Define "destroyed". tmp is created on the stack. If you were to call another method after calling f(), it's possible that tmp would get overwritten, but otherwise there's no reason for the compiler to overwrite the stack just to tidy things up. What you're doing is considered dangerous, however. – Pete Feb 15 '13 at 15:22
  • 1
    Please choose either C or C++. – Bartek Banachewicz Feb 15 '13 at 15:23
  • @Pete Wouldn't that be better as an answer instead of a comment? Anyway, "considered dangerous" is putting it too mildly, I'd just say, "incorrect code". – hyde Feb 15 '13 at 15:25

4 Answers4

3

What you are doing is undefined behaviour, you have a pointer pointing to a memory location that may or may not be in tact. This is bad.

What is actually happening is that you char buffer tmp is located on the stack frame for function f(), when that function returns the data is left on the stack to be over written by future stack frame.

The correct way of doing what you have done is simply.

std::string f() {
     std::string str ("abcdefg");
     std::cout << str << '\n';
     return str;
}
int main() {
    std::string s=f();
    std::cout << s << '\n';
}
Fingolfin
  • 5,363
  • 6
  • 45
  • 66
111111
  • 15,686
  • 6
  • 47
  • 62
1

You are invoking undefined behaviour. The pointer may or may not be valid after f() scope closes.

Bartek Banachewicz
  • 38,596
  • 7
  • 91
  • 135
  • that explains cout << x , however printing characters in the for loop does not print the same character array. – Pungs Feb 15 '13 at 15:24
  • You are doing a lot of things that are considered bad or plainly wrong. Mind to tell what's the reason? – Bartek Banachewicz Feb 15 '13 at 15:25
  • @Pungs Yes, because the location on the stack have been replaced by another data, when you did `cout << x << endl;` This code replaced the data on the stack with other data, which are not printable – banuj Feb 15 '13 at 15:26
  • that explains, i was puzzled because first call on printing worked, however subsequent one do not. The cout function calls overwrites the stack. – Pungs Feb 15 '13 at 16:44
0

You are invoking UB. You access memory that is no longer allocated for your program. It is mere luck it works on the first print.

Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
0

In your program x is defined to be a global pointer which is initialized with an address on the stack as char temp[100] is allocated on the stack. When the function f returns, the stack pointer is decremented. However, x will continue to point to the same memory location which will not have the correct values and hence, incorrect output is observed.

Ganesh
  • 5,880
  • 2
  • 36
  • 54