Jump to content
  • Advertisement
Sign in to follow this  

string literals in c

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey guys. having trouble coming to grips with how c deals with string literals written into the code. say, you have a function void SomeFunction ( char * Stuff) SomeOtherVariable = malloc (sizeof(char) * (strlen(Stuff) + 1)) ; strcpy (SomeOtherVariable, Stuff) ; } ; and you call it like SomeFunction ("Bob") ; what happens to bob? It works, so its apparent that the compiler is setting aside room for the temporary variable, and passing a pointer to it to SomeOtherFunction. But will the compiler automatically free it as well, or will it just be lost?

Share this post


Link to post
Share on other sites
Advertisement
In C the rule is that whenever you malloc something you must free() it.

In this case it copies everything in memory from the byte represented by 'stuff' to the next '\0' character and copies it into the memory allocated.

Add the line:

free( SomeOtherVariable );

at the end of the function.

What happens to Bob?

Well calling the function passing in "Bob" is exactly the same as this:

char name[] = "Bob";

SomeFunctions( name );

EDIT: It's probably a typo but there is no semi-colon on the closing brace of the function.

ace

Share this post


Link to post
Share on other sites
all literal are statically allocated on string table in the exe so when you exit all memory is released. If you malloc you have to delete the memory you handle. An interesting question is: what happen to
SomeFunction("Bob");
SomeFunction("Bob");
we have 2 literals, the compiler will refer to the same string in string table or not? The standard does not make a rule for identical string so it's up to compiler manufacturer. In order to test what your compiler make try:
std::cout << (void*)"Bob" << '|' << (void*)"Bob";

Share this post


Link to post
Share on other sites
ah,cheers.
i realise that i have to free stuff i explicitally malloc,i was just showing how you might copy a string out of a literal and use it for something else.

Share this post


Link to post
Share on other sites
WARNING: For backwards compatibility with C, C++ lets you refer to those string literal constants as if they were non-constant, but actually trying to modify them could blow up at runtime (in particular if you try to increase their length; but it might not be allowed at all, because it's part of "the executable" as represented in memory).

Share this post


Link to post
Share on other sites
A string literal is stored "in code". Optomization may combine like strings together, or place them in a table, but effectively these strings are stored in the same way that the executable machine code operators are stored. When you compile them, they get placed in the binary, and when you start the program, they're loaded into memory and persist until the end of the program. Altering them is like altering the machine code itself, and a big No No. If you need strings to alter, set aside space for them, because the functionality of a string literal is undefined when you use it as anything else.

Share this post


Link to post
Share on other sites
For compatibility (and to avoid rewriting 1G lines of code) in C++ you can write this legal instruction:
char* flarp = "flarp";
a good C++ style (and the Author of the language) says to avoid this implicit convertion cause char* is a standard pointer and you can change it as well, write this way:
const char flarp[] = "flarp";
so you have no problem at all
trying to modify a string literal is bad cause the segment of memory in wich reside is flagged as read-only (not all the time, depends on a lot of options). In win32 env you can use the VirtualQueryEx function and take ownership of this segment, then you can write it as normal memory (without exceeding the '\0' obviously). But this is outside the scope of the current discussion. If you want to use the literal in various places of code you need to do a very simple thing: declare in a *.h and use it where you want. No need to malloc() if you don't need to modify the contents.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!