optLog wrote:I'm sorry everyone I guess I've been asking the wrong question. what I'm trying to find out is how literals are stored in memory.
IIRC they're usually stored on the stack. Sometimes the compiler may optimize it differently, but that is (I think) the general case. There is no need to worry about memory management with constant literals.
If a literal is stored on the stack then that would mean that it would be deleted along with the rest of the non static local variables right?
btw, should I change the name of the thread? and if so, how would I do that?
They aren't "deleted" they go out of scope. And yes, if you want to change the name of the thread go to your original post and press edit and change the subject line.
optLog wrote:I'm sorry everyone I guess I've been asking the wrong question. what I'm trying to find out is how literals are stored in memory.
IIRC they're usually stored on the stack. Sometimes the compiler may optimize it differently, but that is (I think) the general case. There is no need to worry about memory management with constant literals.
If a literal is stored on the stack then that would mean that it would be deleted along with the rest of the non static local variables right?
btw, should I change the name of the thread? and if so, how would I do that?
They aren't "deleted" they go out of scope. And yes, if you want to change the name of the thread go to your original post and press edit and change the subject line.
So literals act more like static local variable in that they keep there values even after the function end, is that about right?
So literals act more like static local variable in that they keep there values even after the function end, is that about right?
No. When I say they go out of scope it means the memory where they are stored is marked as "ready to be written over." If you access the variable after it goes out of scope it MIGHT have the value you expect, it might be written over and be random garbage.
void someFunction(char* string)
{
string[3] = 'u';
}
int main()
{
someFunction("this is a string");
return 0;
}
When you call 'someFunction' a pointer to the string is put on the stack, this pointer points somewhere in the read-only region.
So the reason this program fails has nothing to do with the scope of the string (it will stay in memory as long as your program runs).
The reason it fails is the fact that the section of memory it is in, is read-only so you will get a segmentation fault.
void someFunction(char* string)
{
string[3] = 'u';
}
int main()
{
someFunction("this is a string");
return 0;
}
When you call 'someFunction' a pointer to the string is put on the stack, this pointer points somewhere in the read-only region.
So the reason this program fails has nothing to do with the scope of the string (it will stay in memory as long as your program runs).
The reason it fails is the fact that the section of memory it is in, is read-only so you will get a segmentation fault.