Page 1 of 2

how literals are stored in memory

Posted: Wed Oct 06, 2010 4:21 am
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?

Re: question about passing string literals to functions

Posted: Wed Oct 06, 2010 4:24 am
by ismetteren
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

Posted: Wed Oct 06, 2010 4:30 am
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?

Re: question about passing string literals to functions

Posted: Wed Oct 06, 2010 5:18 am
by Scoody
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

Posted: Wed Oct 06, 2010 3:57 pm
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[]);

Re: question about passing string literals to functions

Posted: Wed Oct 06, 2010 4:20 pm
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.

Re: question about passing string literals to functions

Posted: Wed Oct 06, 2010 11:46 pm
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.

Re: question about passing string literals to functions

Posted: Thu Oct 07, 2010 9:43 am
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.

Re: question about passing string literals to functions

Posted: Thu Oct 07, 2010 12:56 pm
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);

Re: question about passing string literals to functions

Posted: Thu Oct 07, 2010 5:20 pm
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

Re: question about passing string literals to functions

Posted: Thu Oct 07, 2010 5:24 pm
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:

Re: question about passing string literals to functions

Posted: Thu Oct 07, 2010 7:12 pm
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.

Re: question about passing string literals to functions

Posted: Thu Oct 07, 2010 10:42 pm
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.

Re: question about passing string literals to functions

Posted: Thu Oct 07, 2010 10:44 pm
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.

Re: question about passing string literals to functions

Posted: Thu Oct 07, 2010 11:00 pm
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?