Results 1 to 9 of 9
Why is that valid() returns a valid string? I thought the variables created in a function cease to exist when the function exits(stack frame is released), so why does main() ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 07-29-2012 #1Just Joined!
- Join Date
- Jul 2012
- Posts
- 3
Stack Frame | Automatic Variables
Why is that valid() returns a valid string? I thought the variables created in a function cease to exist when the function exits(stack frame is released), so why does main() print the correct value(i.e., "hello") ?
Code:#include <stdlib.h> #include <stdio.h> const char* valid() { return "hello"; } int main(int argc, char** argv) { printf("%s",valid()); getchar(); return 0; }
- 07-30-2012 #2Just Joined!
- Join Date
- May 2012
- Location
- Tampere, Finland
- Posts
- 21
You return a "hello". The return value of the valid() function is "hello" and you immediately print the returned value with printf. You do not even store it into a variable at any point.
if you would try to save the word "hello" to some variable inside the function likeand then try to print that variable in the main function like :Code:char* helloString = "hello";
it would not work since the variable does not exist inside the main(), only in valid().Code:printf("%s",helloString);
But if you return the variable it would work.
But again here, you need to print valid(), not helloStringCode:#include <stdlib.h> #include <stdio.h> const char* valid() { const char * helloString = "hello"; return helloString; } int main(int argc, char** argv) { printf("%s",valid()); getchar(); return 0; }
Please enlighten me if i understood your questing wrongLast edited by Gjordis; 07-30-2012 at 11:02 AM.
- 07-30-2012 #3Just Joined!
- Join Date
- Dec 2011
- Posts
- 21
when you use with pointers the code works well unless the address is invalid.
whentry same thing using arrays instead of pointers..
i mean instead of
char *str =" hello"; use char str[] = "hello";
then it will through the warning you are expecting and print some garbage value.
as far as pointers is concerned, the strings are stored in R/O location which is out side the stack , so the code works fine.
experts can correct me if i am worng.
- 07-30-2012 #4Just Joined!
- Join Date
- Jul 2012
- Posts
- 3
Gjordis, I meant something a little different. Thanks anyway though.
Strange, it won't let me print a char[] str. I guess it has to be in a R/O location outside of the stack, unless the string is dumped on the stack with the main stack frame. Another question is, I could understand why const string go to a special R/O location, but why char*?
- 07-31-2012 #5Just Joined!
- Join Date
- May 2012
- Location
- Tampere, Finland
- Posts
- 21
char[] unlike char* is not a string. You need to allocate a space for it. for example
. Char* points to a certain address in your memory which represents a starting character. The output of this variable is everything between the starting character until a null terminator is reached. char* str = "hello"; holds a null terminator which is automatically placed after o like "hello\0". Char[] does not work like this and you need to iterate through it for example in a for loop.Code:char str[6]
And concerning the example you put in the first post. Pointers have nothing to do with it working or not, since you access a return value instead of an variable. Changing the return type of the function to something else, for example int, and returning 1 would still work.
I'm probably still not understanding it correctly, what you are asking.
- 07-31-2012 #6Just Joined!
- Join Date
- Jul 2012
- Posts
- 3
My question is basically, where is the return value stored? I was almost convinced it should be on the stack, but if it is, why is still accessible after the valid() stack frame is removed? The closest answer we've got is stored in some R/O location, then, would this R/O location be on the heap or something, or the variable is pushed on the main() stack frame?
Regarding the char[], yes I would need to add a '\0' at the end.
- 07-31-2012 #7Just Joined!
- Join Date
- May 2012
- Location
- Tampere, Finland
- Posts
- 21
For the time that its possible to account for its existence in that code snippet, it exists in the memory of the function to the end of the function, then it exists in some temporary memory used by the printf output stream. I wont go into details concerning the structure of the actual c/c++ language from where you should find where it exists.
Return values are supposed to be the thing a function leaves behind when it dies. At the instant return value is presented to the main function, it is passed to a printstream in this example. Because this is a pointer to a memory address, when the function exists, the same information of this address is now known to the printf()-function, It reads from the address until the null terminator.
- 07-31-2012 #8Just Joined!
- Join Date
- May 2012
- Location
- Tampere, Finland
- Posts
- 21
printf.jpg
This represents the timeline of the software. All items on same level can be considered simultaneous to a human. Information passed between simultaneous events are usually stored in different data structures defined in the implementation of the module. For example printf stores the char* it gets in some datatype you can find out from its source code from it's libraries.
- 08-04-2012 #9
So. Let's walk through what happens. You call valid(). That has a string "hello", and returns its memory address. Your main function prints out whatever is at that memory address.
If the memory address was within the frame pointer of the valid call, this wouldn't work (or, rather, would be undefined). However, this is slightly different. Static strings are not stored on the stack. They are stored in the text section of the binary. So it doesn't matter what the frame is, because that memory address is in a whole separate part of memory.
If you were manually constructing the string in the function call, you would have a problem.
Does that make sense?


Reply With Quote

