how literals are stored in memory

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

User avatar
short
ES Beta Backer
ES Beta Backer
Posts: 548
Joined: Thu Apr 30, 2009 2:22 am
Current Project: c++, c
Favorite Gaming Platforms: SNES, PS2, SNES, SNES, PC NES
Programming Language of Choice: c, c++
Location: Oregon, US

Re: question about passing string literals to functions

Post 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. :)
My github repository contains the project I am currently working on,
link: https://github.com/bjadamson
optLog
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 18
Joined: Wed Oct 06, 2010 3:40 am

Re: how literals are stored in memory

Post 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?
User avatar
short
ES Beta Backer
ES Beta Backer
Posts: 548
Joined: Thu Apr 30, 2009 2:22 am
Current Project: c++, c
Favorite Gaming Platforms: SNES, PS2, SNES, SNES, PC NES
Programming Language of Choice: c, c++
Location: Oregon, US

Re: how literals are stored in memory

Post 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.
My github repository contains the project I am currently working on,
link: https://github.com/bjadamson
User avatar
Amarant
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 34
Joined: Wed Nov 05, 2008 9:52 am

Re: how literals are stored in memory

Post 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.
177
User avatar
short
ES Beta Backer
ES Beta Backer
Posts: 548
Joined: Thu Apr 30, 2009 2:22 am
Current Project: c++, c
Favorite Gaming Platforms: SNES, PS2, SNES, SNES, PC NES
Programming Language of Choice: c, c++
Location: Oregon, US

Re: how literals are stored in memory

Post 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
My github repository contains the project I am currently working on,
link: https://github.com/bjadamson
Post Reply