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: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?Ginto8 wrote: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.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.
btw, should I change the name of the thread? and if so, how would I do that?
how literals are stored in memory
Moderator: Coders of Rage
- short
- 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
My github repository contains the project I am currently working on,
link: https://github.com/bjadamson
link: https://github.com/bjadamson
Re: how literals are stored in memory
So literals act more like static local variable in that they keep there values even after the function end, is that about right?short wrote: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: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?Ginto8 wrote: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.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.
btw, should I change the name of the thread? and if so, how would I do that?
- short
- 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
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.So literals act more like static local variable in that they keep there values even after the function end, is that about right?
My github repository contains the project I am currently working on,
link: https://github.com/bjadamson
link: https://github.com/bjadamson
Re: how literals are stored in memory
String literals are NOT on the stack, they are loaded into a read-only region of memory when you open an executable.
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.
Code: Select all
void someFunction(char* string)
{
string[3] = 'u';
}
int main()
{
someFunction("this is a string");
return 0;
}
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
- short
- 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
learned something I thought I knew, hahaAmarant wrote:String literals are NOT on the stack, they are loaded into a read-only region of memory when you open an executable.
When you call 'someFunction' a pointer to the string is put on the stack, this pointer points somewhere in the read-only region.Code: Select all
void someFunction(char* string) { string[3] = 'u'; } int main() { someFunction("this is a string"); return 0; }
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.
My github repository contains the project I am currently working on,
link: https://github.com/bjadamson
link: https://github.com/bjadamson