How safe and reliable are C ++ String literals?

So, I want to better understand how string literals work in C ++. I am mostly concerned about situations where you assign the address of a string literal to a pointer and pass it. For example:

char* advice = "Don't stick your hands in the toaster."; 

Now let's say that I just pass this line by copying pointers to the time the program runs. Of course, this is probably not a good idea, but I'm curious what is actually going on behind the scenes.

In another example, suppose we create a function that returns a string literal:

 char* foo() { // function does does stuff return "Yikes!"; // somebody feeble attempt at an error message } 

Now let's say that this function is called very often, and the string literal is used only in half the cases when it called:

 // situation #1: it just randomly called without heed to the return value foo(); // situation #2: the returned string is kept and used for who knows how long char* retVal = foo(); 

In the first situation, what actually happens? Is a string just created but not used and never freed?

In the second situation, the string will be maintained until the user finds the need for it? What happens when it is no longer needed ... will this memory be freed then (if nothing is pointed to this space)?

Don't get me wrong, I don't plan on using string literals like this. I plan to use a container to check my strings (maybe std :: string). I just want to know if these situations can cause problems for memory management or data corruption.

+7
c ++ string memory-management string-literals
source share
2 answers

String literals are of type const char[N] (where N is the length + 1) and are statically distributed. You do not need to worry about memory problems; if your program uses a string, it is processed for you and is located somewhere in the program memory (usually read-only).

That is, they are "the same":

 static const char str[] = "a string"; "a string" 

When you point to a string literal, you point to the first character in the array. In fact, since the type is const char[] , it is safe to point to it using const char* . Converting from a string literal to char* is deprecated and unsafe.

 // the "same" static const char str[] = "a string"; const char* strPtr = str; // decays const char* s1 = "a string"; char* s2 = "a string"; // allowed, implicit const_cast *s1 = 'A'; // not allowed, it const *s2 = 'B'; // allowed, it not const (but leads to undefined behavior) 
+22
source share

First, declare the return value of foo as const, because string literals are constants that cannot be changed without causing the scary "undefined" behavior. This will then force any pointers that use the return value of foo to also be declared as const and could potentially limit the damage that can (usually unintentionally) be done. String literals are stored in the "text" area of ​​the binary executable - they are not created as such at run time.

+1
source share

All Articles