Page 2 of 2

Re: question about passing string literals to functions

Posted: Fri Oct 08, 2010 12:13 am
by short
optLog wrote:
Ginto8 wrote:
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. :)

Re: how literals are stored in memory

Posted: Fri Oct 08, 2010 1:04 am
by optLog
short wrote:
optLog wrote:
Ginto8 wrote:
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?

Re: how literals are stored in memory

Posted: Fri Oct 08, 2010 2:00 pm
by short
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.

Re: how literals are stored in memory

Posted: Sun Oct 10, 2010 11:00 am
by Amarant
String literals are NOT on the stack, they are loaded into a read-only region of memory when you open an executable.

Code: Select all

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.

Re: how literals are stored in memory

Posted: Sun Oct 10, 2010 10:41 pm
by short
Amarant wrote:String literals are NOT on the stack, they are loaded into a read-only region of memory when you open an executable.

Code: Select all

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.
learned something I thought I knew, haha