This is a really basic question but I did not find a clear answer. I try to understand how string literals in C
work.
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
char *str = "Hello World!";
str = "Reassign str.";
char *str2 = "Something.";
str = strdup(str2);
free(str);
exit(EXIT_SUCCESS);
}
When I do str = "Reassign str."
or str = strdup(str2)
to a string literal what happens to the Hello World
string? Is it part of the executable, does it go out of scope, is it's memory freed after I reassign str
?
String literals have static storage duration. They are not destroyed and have the same addresses during the program execution.
In these statements
str = "Reassign str.";
//...
str = strdup(str2);
pointer str
is just reassigned by some other addresses. In the first statement it gets the address of the first character of string literal "Reassign str." and in the second statement it gets the address of the dynamically allocated memory for the character array that contains a copy of the string literal pointed to by str2.
According to the C Standard (6.4.5 String literals)
6 In translation phase 7, a byte or code of value zero is appended to each multibyte character sequence that results from a string literal or literals.78) The multibyte character sequence is then used to initialize an array of static storage duration and length just sufficient to contain the sequence. For character string literals, the array elements have type char, and are initialized with the individual bytes of the multibyte character sequence.
And (6.2.4 Storage durations of objects)
3 An object ...has static storage duration. Its lifetime is the entire execution of the program and its stored value is initialized only once, prior to program startup
As for the string literals themselves then you may not change them. Any attempt to change a string literal results in undefined behaviour of the program.
From the C Standard (6.4.5 String literals)
7 It is unspecified whether these arrays are distinct provided their elements have the appropriate values. If the program attempts to modify such an array, the behavior is undefined.
For example you may not write
char *str = "Hello World!";
*str = 'h';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With