Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

character string memory allocation

Status
Not open for further replies.

hughLg

Programmer
Feb 18, 2002
136
MY
in the function:

Code:
void function()
{
   puts("hello world...");
}

how's the character string "hello world..." allocated in memory? is the momory can be freed properly if i use like that? or i have to use this:

Code:
void function()
{
   static const char s[] = "Hello world...";
   puts(s);
}

or declare that character string as a global constant or variable will make it better?

and 1 thing, when i declare a character pointer:

Code:
{
   char *s = "hello world...";
}

in this case, we're legally access the value of this string, but cannot modify it. how is the memory is allocated? the pointer is just set to point to a beginning of that string, so is the memory allocated for it? and am i have to release its memory to return memory...
 
Dennis,

There are four places that variables are stored.
1) The data segment (globals, static, const)
2) The stack (local variables)
3) The heap (malloc)
4) Processor registers (compiler-chosen local variables)

I believe that a literal string will always be in the data segment, i.e. the actual characters.

This will then be referenced by a pointer:

's' in your second example is a pointer to a constant array of chars. In other words
Code:
s[n] = foo
is not allowed. 's' would then be a local varaible.

's' in your third example is a local variable.

Mark.
 
more rarely...
5) In the code segment. I don't know about c, but in assembly some people put their constant data strings in the code & jmp around it. It jarred me when I first saw it but it does make some sense for readability.

when in doubt look at the assember output.

Also note that the c compiler is going to "optimize away" some of the implied pointers & stuff. It's what they get paid to do. So in the 3rd example, the address may not actually exist as a distinct entity distinct from the code. & the loader would then determine the final address for the string reference.
 
s in my second example is a array of constant character, but not a pointer.

do you tell me why some person prefer to use static character pointer like in my third example than mine?
 
Dennis,

You are right. s is an arrary of const chars, my mistake. Since arrays and pointer are so similar in C I sometimes use them synonomously.

Now to your question.

In your second example s is an array so

Code:
s = foo ; [code]

is illegal.

In your third example s is a pointer so

[code]s = foo ;

is legal.

Most people prefer to use the thrid because is more closely represents what is acutally going on. You have a pointer which references memory which is allocated elsewhere (in this case by the compiler). Use the array syntax to actually allocate space that you can use, for example

Code:
void function()
{
   char s[] = "Hello world...";
   puts(s);
}

will allocate just enough space to store the string and a null character. When the function exits the space disappears.

Mark.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top