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

optLog
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 18
Joined: Wed Oct 06, 2010 3:40 am

how literals are stored in memory

Post by optLog »

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?
Last edited by optLog on Fri Oct 08, 2010 1:00 am, edited 1 time in total.
User avatar
ismetteren
Chaos Rift Junior
Chaos Rift Junior
Posts: 276
Joined: Mon Jul 21, 2008 4:13 pm

Re: question about passing string literals to functions

Post by ismetteren »

My guess is that they are declared on the stack and get deleted when they go out of scope.
Image ImageImage Image
optLog
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 18
Joined: Wed Oct 06, 2010 3:40 am

Re: question about passing string literals to functions

Post by optLog »

ismetteren wrote:My guess is that they are declared on the stack and get deleted when they go out of scope.
would the function ending cause them to go out of scope?
Scoody
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 65
Joined: Fri Feb 06, 2009 2:07 pm

Re: question about passing string literals to functions

Post by Scoody »

Since you're just passing a pointer to the string, only the pointer gets removed when the function ends.
optLog
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 18
Joined: Wed Oct 06, 2010 3:40 am

Re: question about passing string literals to functions

Post by optLog »

Scoody wrote:Since you're just passing a pointer to the string, only the pointer gets removed when the function ends.
So if you didn't deallocate the memory or reference it then it would be leaked right?
also:
would these two functions act the same way?

Code: Select all

void function(char* string);
void function(char string[]);
User avatar
Falco Girgis
Elysian Shadows Team
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

Post by Falco Girgis »

optLog wrote:
Scoody wrote:Since you're just passing a pointer to the string, only the pointer gets removed when the function ends.
So if you didn't deallocate the memory or reference it then it would be leaked right?
also:
would these two functions act the same way?

Code: Select all

void function(char* string);
void function(char string[]);
Whoah, whoah, whoah. Calm the fuck down here, cowboys.

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);
Stack allocation:

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;
}
Heap allocation:

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;
}
Note that the function has nothing to do with either, since you're simply passing a memory address.
would these two functions act the same way?

Code: Select all

void function(char* string);
void function(char string[]);
Yes, and this is a fundamental concept of C that you need to become familiar with. That's why I can do this:

Code: Select all

char string[100];
char *charPointer;

charPointer = string;
charPointer[0] = 'h';
charPointer[1] = 'e';
//etc.
I am now able to use the character pointer as the array itself once it points to the first element.
optLog
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 18
Joined: Wed Oct 06, 2010 3:40 am

Re: question about passing string literals to functions

Post by optLog »

let me rephrase my first question a little bit.

Code: Select all

void someFunction(char* string);
int main()
{
  someFunction("this is a string");
  return 0;
}
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.

Code: Select all

void function(char* string);
int main()
{
 function("this is a string");
 return 0;
}
void function(char* string)
{
  string[3] = 'u';
}
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.
User avatar
dandymcgee
ES Beta Backer
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

Post by dandymcgee »

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.
It is passed as a const char array. You cannot edit constants at run-time.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: question about passing string literals to functions

Post by avansc »

dandymcgee wrote:
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.
It is passed as a const char array. You cannot edit constants at run-time.
Correct, but it also only has scope for that function. and yes, its on the stack. (if memory serves me correctly);
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
eatcomics
ES Beta Backer
ES Beta Backer
Posts: 2528
Joined: Sat Mar 08, 2008 7:52 pm
Location: Illinois

Re: question about passing string literals to functions

Post by eatcomics »

avansc wrote:(stuff)

Correct, but it also only has scope for that function. and yes, its on the stack. (if memory serves me correctly);
Ha... stack, memory, good one
Image
User avatar
Ginto8
ES Beta Backer
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

Post by Ginto8 »

eatcomics wrote:
avansc wrote:(stuff)

Correct, but it also only has scope for that function. and yes, its on the stack. (if memory serves me correctly);
Ha... stack, memory, good one
.........
:nono:
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.
User avatar
Falco Girgis
Elysian Shadows Team
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

Post by Falco Girgis »

optLog wrote:let me rephrase my first question a little bit.

Code: Select all

void someFunction(char* string);
int main()
{
  someFunction("this is a string");
  return 0;
}
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?
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.

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.
optLog
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 18
Joined: Wed Oct 06, 2010 3:40 am

Re: question about passing string literals to functions

Post by optLog »

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.
User avatar
Ginto8
ES Beta Backer
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

Post by Ginto8 »

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.
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.
optLog
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 18
Joined: Wed Oct 06, 2010 3:40 am

Re: question about passing string literals to functions

Post by optLog »

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?
Post Reply