问题
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?
回答1:
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?
回答2:
Because your local variable str
is going out of scope when you leave the getString
function.
回答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.
回答4:
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 */
}
回答5:
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.
回答6:
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.
回答7:
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();
}
回答8:
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.
来源:https://stackoverflow.com/questions/25402282/what-will-be-the-output-of-following-c-program