how literals are stored in memory
Moderator: Coders of Rage
how literals are stored in memory
When you pass a string literal to a function like this
void function(char* string);
what happens to the memory allocated to store the literal?
the reason I ask is because as far as I understand it, the pointer 'string' would be deleted after 'function' ends. so if you didn't assign another pointer to that memory and you didn't deallocate it, would cause memory leak? or are function parameters treated differently then local variables?
void function(char* string);
what happens to the memory allocated to store the literal?
the reason I ask is because as far as I understand it, the pointer 'string' would be deleted after 'function' ends. so if you didn't assign another pointer to that memory and you didn't deallocate it, would cause memory leak? or are function parameters treated differently then local variables?
Last edited by optLog on Fri Oct 08, 2010 1:00 am, edited 1 time in total.
- ismetteren
- Chaos Rift Junior
- Posts: 276
- Joined: Mon Jul 21, 2008 4:13 pm
Re: question about passing string literals to functions
My guess is that they are declared on the stack and get deleted when they go out of scope.
Re: question about passing string literals to functions
would the function ending cause them to go out of scope?ismetteren wrote:My guess is that they are declared on the stack and get deleted when they go out of scope.
Re: question about passing string literals to functions
Since you're just passing a pointer to the string, only the pointer gets removed when the function ends.
Re: question about passing string literals to functions
So if you didn't deallocate the memory or reference it then it would be leaked right?Scoody wrote:Since you're just passing a pointer to the string, only the pointer gets removed when the function ends.
also:
would these two functions act the same way?
Code: Select all
void function(char* string);
void function(char string[]);
- Falco Girgis
- Elysian Shadows Team
- Posts: 10294
- Joined: Thu May 20, 2004 2:04 pm
- Current Project: Elysian Shadows
- Favorite Gaming Platforms: Dreamcast, SNES, NES
- Programming Language of Choice: C/++
- Location: Studio Vorbis, AL
- Contact:
Re: question about passing string literals to functions
Whoah, whoah, whoah. Calm the fuck down here, cowboys.optLog wrote:So if you didn't deallocate the memory or reference it then it would be leaked right?Scoody wrote:Since you're just passing a pointer to the string, only the pointer gets removed when the function ends.
also:
would these two functions act the same way?Code: Select all
void function(char* string); void function(char string[]);
You are passing the MEMORY ADDRESS of the FIRST ELEMENT of the array to the function. Whether it needs to be deleted or not depends on how it was allocated:
Code: Select all
void StringFunction(char *string);
Code: Select all
int main() {
char string[100]; //allocated on the STACK
StringFunction(string); //passing memory address to first element
//string is removed from the stack implicitly when the program exits.
return 0;
}
Code: Select all
int main() {
char *string = malloc(sizeof(char)*10); //allocate the array dynamically on the heap
StringFunction(string); //still passing the memory address, it makes absolutely no difference.
free(string); //You are responsible for deleting dynamic memory now.
return 0;
}
Yes, and this is a fundamental concept of C that you need to become familiar with. That's why I can do this:would these two functions act the same way?Code: Select all
void function(char* string); void function(char string[]);
Code: Select all
char string[100];
char *charPointer;
charPointer = string;
charPointer[0] = 'h';
charPointer[1] = 'e';
//etc.
Re: question about passing string literals to functions
let me rephrase my first question a little bit.
how is the string literal "this is a string" handled in memory?
Is it allocated as part of the main function or is memory dynamically allocated for it when the main function calls someFunction?
Is the same copy of "this is a string" sent to someFunction or is a copy of it made and sent to someFunction?
EDIT: I created a simple program to see what would happen if I tried to edit a string literal passed to a function.
When I ran the program it crashed which leads me to believe that "this is a string" is allocated as part of the main function or something because if a copy of "this is a string" was being passed to the function I can't think of a reason why changing one of it's values would cause the program to crash.
Code: Select all
void someFunction(char* string);
int main()
{
someFunction("this is a string");
return 0;
}
Is it allocated as part of the main function or is memory dynamically allocated for it when the main function calls someFunction?
Is the same copy of "this is a string" sent to someFunction or is a copy of it made and sent to someFunction?
EDIT: I created a simple program to see what would happen if I tried to edit a string literal passed to a function.
Code: Select all
void function(char* string);
int main()
{
function("this is a string");
return 0;
}
void function(char* string)
{
string[3] = 'u';
}
- dandymcgee
- ES Beta Backer
- Posts: 4709
- Joined: Tue Apr 29, 2008 3:24 pm
- Current Project: https://github.com/dbechrd/RicoTech
- Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
- Programming Language of Choice: C
- Location: San Francisco
- Contact:
Re: question about passing string literals to functions
It is passed as a const char array. You cannot edit constants at run-time.optLog wrote: When I ran the program it crashed which leads me to believe that "this is a string" is allocated as part of the main function or something because if a copy of "this is a string" was being passed to the function I can't think of a reason why changing one of it's values would cause the program to crash.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches!
Re: question about passing string literals to functions
Correct, but it also only has scope for that function. and yes, its on the stack. (if memory serves me correctly);dandymcgee wrote:It is passed as a const char array. You cannot edit constants at run-time.optLog wrote: When I ran the program it crashed which leads me to believe that "this is a string" is allocated as part of the main function or something because if a copy of "this is a string" was being passed to the function I can't think of a reason why changing one of it's values would cause the program to crash.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Dad, "Yea well I have a fan belt in street fighting"
Re: question about passing string literals to functions
Ha... stack, memory, good oneavansc wrote:(stuff)
Correct, but it also only has scope for that function. and yes, its on the stack. (if memory serves me correctly);
- Ginto8
- ES Beta Backer
- Posts: 1064
- Joined: Tue Jan 06, 2009 4:12 pm
- Programming Language of Choice: C/C++, Java
Re: question about passing string literals to functions
.........eatcomics wrote:Ha... stack, memory, good oneavansc wrote:(stuff)
Correct, but it also only has scope for that function. and yes, its on the stack. (if memory serves me correctly);
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
- Falco Girgis
- Elysian Shadows Team
- Posts: 10294
- Joined: Thu May 20, 2004 2:04 pm
- Current Project: Elysian Shadows
- Favorite Gaming Platforms: Dreamcast, SNES, NES
- Programming Language of Choice: C/++
- Location: Studio Vorbis, AL
- Contact:
Re: question about passing string literals to functions
I don't think you understood my response. I told you EXACTLY how it's handled in memory: however you allocated it. You're simply passing a memory address to the function. It's not like anything is being copied or any second string is being created.optLog wrote:let me rephrase my first question a little bit.how is the string literal "this is a string" handled in memory?Code: Select all
void someFunction(char* string); int main() { someFunction("this is a string"); return 0; }
Is it allocated as part of the main function or is memory dynamically allocated for it when the main function calls someFunction?
Is the same copy of "this is a string" sent to someFunction or is a copy of it made and sent to someFunction?
And any string literal becomes a CONSTANT character array at compile-time. You cannot alter a constant. Try passing in a character array, editing it within the function, then printing it once you leave. You'll see that you altered your original array, because it was passed by reference.
Re: question about passing string literals to functions
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.
- Ginto8
- ES Beta Backer
- Posts: 1064
- Joined: Tue Jan 06, 2009 4:12 pm
- Programming Language of Choice: C/C++, Java
Re: question about passing string literals to functions
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.
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
Re: question about passing string literals to functions
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?