3
char *getString()
{
    char str[] = "Will I be printed?";    
    return str;
}
int main()
{
    printf("%s", getString());
    getchar();
}

Shouldn't the output be "Will I be printed?" ? Instead, the output is coming out to be some garbage value. Why is it so?

cst
  • 53
  • 5
  • http://stackoverflow.com/questions/21415700/is-it-ok-to-reference-an-out-of-scope-local-variable-within-the-same-function – phuclv Aug 20 '14 at 10:32

8 Answers8

6
char str[] = "Will I be printed?";   

is a local declaration. It is limited with in the function getString(). when you leave the function, str[] will be collapsed.

so you are trying to print the data at it. Obviously you will Garbage value!

To avoid this -

char *str = "Will I be printed?";

Now str will be stored in code memory, when you leave the function, str will not be collapsed. now it will print Will I be printed?

Sathish
  • 3,740
  • 1
  • 17
  • 28
3

Because your local variable str is going out of scope when you leave the getString function.

nvoigt
  • 75,013
  • 26
  • 93
  • 142
3

You are returning the address of a local variable, this is forbidden, but you can return a string literal in this way:

char *getString()
{
    char *str = "Will I be printed?";
    return str; /* read only */
}

or

char *getString()
{
    return "Will I be printed?"; /* read only */
}

or

char *getString()
{
    static char str[] = "Will I be printed?";
    return str; /* read - write */
}
David Ranieri
  • 39,972
  • 7
  • 52
  • 94
3

The value of str will be free once you get out of the function,instead you should use :

char *str = "Will I be printed?";    
return str;

you should get some knowledge about the stack and heap.

FisherMartyn
  • 826
  • 1
  • 8
  • 17
2

The str array lives inside the getString() function (local scope). As with all local variables, storage for it will be usually allocated on the stack.

Hence, upon returning, it will be automatically deallocated.

Roberto Reale
  • 4,247
  • 1
  • 17
  • 21
2

You are declaring a local variable in the scope of the function getString. When the function ends, just before it returns the value, the memory for that variable str is released and cleaned up, because the scope has ended.

Now the function returns the address to this cleaned up memory. In many cases, the contents are already overwritten by some other process by the time you access the contents. So it looks like garbage values.

metacubed
  • 7,031
  • 6
  • 36
  • 65
2

Here char str[] is local variable (auto). The scope of the variable is limited to function getString in your case. So accessing the address of str outside of function getString will cause undefined behavior.

But the code can be corrected as like below

char *getString()
{
    char *str = "Will I be printed?";    
    return str;
}
int main()
{
    printf("%s", getString());
    getchar();
}
mahendiran.b
  • 1,315
  • 7
  • 13
1

There is a big difference among these three functions

char *getString()
{
    char str[] = "Will I be printed?";    
    return str;
}

char *getString()
{
    static char str[] = "Will I be printed?";    
    return str;
}

char *getString()
{
    char *str = "Will I be printed?";    
    return str;
}

Using the first function results in undefined behaviour though in most cases there will be outputed string

Will I be printed?

because for this simple program the stack might be not overwritten.

The last two functions are well-defined because the character array and string literal pointer to which is returned have static storage duration. So after exiting the functions the both objects will be alive.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335