1

Simple program that gets char pointer and puts it to the output.

char * get()
{
    char ss [256];
    sprintf (ss,"%d",1);
    return ss;
}

int _tmain(int argc, _TCHAR* argv[])
{
    char *f = get();
    cout<<f;
    char d[50] ;
    cin>> d;
}

I got only garbage on output. Why?

Cœur
  • 37,241
  • 25
  • 195
  • 267
vico
  • 17,051
  • 45
  • 159
  • 315
  • 1
    possible duplicate of [Can a local variable's memory be accessed outside its scope?](http://stackoverflow.com/questions/6441218/can-a-local-variables-memory-be-accessed-outside-its-scope) – Bo Persson Jul 18 '12 at 15:55

3 Answers3

7

Function is returning the address of a local variable. The code has undefined behaviour and gives unpredicted results.

ss resides on stack and the function get() returns a pointer to it.

char * get()
{
} // Life time of ss ends here

Use std::string instead.

bames53
  • 86,085
  • 15
  • 179
  • 244
Mahesh
  • 34,573
  • 20
  • 89
  • 115
  • 1
    A minor note. I'd use "pointer" instead of "reference" as, in C++, "reference" has its own specific meaning. – Desmond Hume Jul 18 '12 at 15:13
  • @Mahesh Pls explain "use std::string instead". std::string doesn't solve the problem because it can go out of scope, too. The key to the solution is using an allocated buffer/object or a static one, or having the caller supply their own buffer. – Les Jul 18 '12 at 16:46
2

When the get() function returns, the local variable goes out of scope. I.e., it's value becomes undefined.

To solve, you can use...

static char ss[255];
// or  
char *ss = (char *)calloc(1,255);
// or with C++
char *ss = new char[255];

or so on...

You decide the trade-off. With a static variable, every call to get() could change the contents of the buffer. But with an approach involving allocation, your caller needs to free the memory and know whether to use free() or delete. Some approach the problem by supplying the buffer to the function when called, like...

void get(char *buf, int limit);
// or
void get(char *buf, int& limitActual);

Then main thing is that when dealing with strings, in C/C++ (even std::string) you are dealing with memory that has to be managed somehow. With string, be very careful with automatic variables.

Les
  • 10,335
  • 4
  • 40
  • 60
  • If I do "calloc" or "new char[255]" then I must free memory somehow in caller side or it will be done automatically when caller will go out of scope? – vico Jul 19 '12 at 14:30
  • ' ddd'dd ddd sadsadsadsadsa dsaasd – vico Jul 19 '12 at 17:50
  • Correct me if I'm wrong. In case 'void get(char *buf, int& limitActual);' Caller creates variable and sets it length. Function 'get' fills buffer and sets limitActual to filled info length. ilimitActual is always >= than buffer length created by caller. But who needs actualLength - you can always see zero termination character. – vico Jul 19 '12 at 17:56
2

Your array ss[] only exists within the scope of get().
So the pointer to it that you return from get() is invalid as soon as you leave the function.

Tonny
  • 671
  • 5
  • 17