Archived

This topic is now archived and is closed to further replies.

Technical strings question

This topic is 6110 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hello people. I''ve always been confused about allocation strings and passing them to functions which not a good thing since I like to know what''s going on in my code. Wait - that''s a bit cryptic no? Let me put it plainly. I might do something like this : char *pGlobalString = NULL; void someFunction( char *pParamString ) { pGlobalString = pParamString; } void anotherFunction( ) { someFunction( "Har, har! This is a string...really it is." ); } Now a questions jump into my head, though I think might have the answer...I''m not sure. *What happens when the function anotherFunction() ends? Does pGlobalString still point to: "Har, har! This is a string...really it is." ? My guess is that the pointer passed to someFunction() is actually a pointer to the string in the code itself. In other words the memory space of the code must have "Har, har! This is a string...really it is." embedded in it somewhere, no? Unless ofcourse the compiler automatically generates a global to hold the string? And the most important question : Is it safe to do this???? I don''t want the string to be overwritten some time during the execution. Well, Thanx.

Share on other sites
Well, that code would make pGlobalString point to "Har, har! This is a string...really it is." in someFunction(), but as soon as someFunction() ends, the string would go out of scope and be destroyed, therefore pGlobalString would point to an invalid place on the stack - big problems. If you want to change the value of pGlobalString, initialise it with a size ( to hold the string ), and simply use strcpy() whenever you want to change it''s value.

The string "Har, har! This is a string...really it is." is pushed onto the stack when someFunction() is called, and popped off again at the end.

If I''m wrong about any of this, someone please say - this is what I believe anyway

Share on other sites
you dont want to do that. It is not safe. The compiler will allocate memory on the stack when you call someFunction(....) for the string that you passed in. After the function completes, the memory is deallocated.
now you could do something like this
  char *pGlobalString = NULL;void someFunction( char *pParamString ){pGlobalString = pParamString;}void anotherFunction( ){ char* something = new char[100]; strcpy(something, ( "Har, har! This is a string...really it is." );someFunction(something);}

in this case the global now would point to that string, but you have to make sure that you delete the memory when youare done with it
like
delete [] pGlobalString;

"I pity the fool, thug, or soul who tries to take over the world, then goes home crying to his momma."
- Mr. T

Share on other sites
In practice very few compilers would allocate a string-literal on the stack. It''s simply too expensive. Instead string-literals are typically allocated statically in the ''.data'' segment.

For example Therian''s little code snippet would work just fine in most compilers (all the compilers I''ve ever heard of, at least).

Share on other sites
although this depends on the compiler , but arent literal strings usually stored in a resource table? especially if the literal string is refrenced more than once.

meaning this memory is always consumed, and technically accessible.

void func1()
{
char* SubPath = "graphics\\c";

SubPath = new char[10]; //point to new memory without deleting the resource

}

void func2()
{
//do stuff
/*it should be technically possible to modify the "graphics\\c" resource, so that when func1 is called, char* SubPath = "xxxxxxxx";
*/
}

Share on other sites
Woh, Interesting stuff guys. Thanx for the pretty advanced replies - good stuff. It would be nice to do it the way I first mentioned simply because it''s easier and I don''t like managing memory for something as small as strings. From the way I understand some of you my theory is kind of correct. The ''.data'' segment and ''resource table'' would indicate something like that (sorry my asm is rusty, but I''ve seen .data in asm code before).
I have MSVC++ so I''m quite confident that it wouldn''t push an odd 80 characters onto the stack unless I was REALLY persuasive
I''ll test the code thouroughly and check whether my computer blows up.

Share on other sites
Dactylos has it right. The string must be stored in a static place in memory when the program is compiled, and the stack certainly isn''t a statcic place in memory. What it will do is pass the memory location of the string literal (which will be save in the .data location since it is considered an "initialized variable") when that function is called (either by placing it in certian argument registers, or placing it on the stack, depending on target processor).

Therian, what you wrote will work; pGlobalString will point to the string you passed in for your example.

Nutts

1. 1
Rutin
19
2. 2
3. 3
JoeJ
15
4. 4
5. 5

• 21
• 19
• 11
• 13
• 17
• Forum Statistics

• Total Topics
631697
• Total Posts
3001764
×