String initializer and read-only section

Suppose I have an array (local to a function) and a pointer

char a[]="aesdf" and char *b="asdf"

My question is that in the first case, the string literal "aesdf" is stored in a read-only section and then copied to a local array or similar

char a[]={'a','e','s','d','f','\0'}; ?

I think that in this case the characters are created directly on the stack, but in the earlier case ( char a[]="aesdf" ) the characters are copied from the read-only section to the local array.

Will there be aesdf for the whole life of the executable?

+2
source share
4 answers

From an abstract and formal point of view, each string literal is an independent nameless object with a static storage duration. This means that initializing char a[] = "aesdf" formally creates a literal object "aesdf" and then uses it to initialize an independent array a , that is, it is not equivalent to char *a = "aesdf" , where a pointer is made to indicate string literal directly.

However, since string literals are nameless objects, the char a[] = "aesdf" does not have access to the independent "aesdf" object before or after initialization. This means that you will not be able to "detect" whether this object existed. The existence (or non-existence) of this object cannot affect the observed behavior of the program. For this reason, the implementation has all the freedom to eliminate the independent "aesdf" object and initializes the array a any other way, which leads to the expected correct result, that is, as char a[] = { 'a', 'e', 's', 'd', 'f', '\0' } or as char a[] = { 'a', 'e', "sdf" } , or as something else.

+5
source

At first:

 char a[]="aesdf"; 

Assuming this is an automatic local variable, it will allocate 6 bytes on the stack and initialize them with the given characters. How this is done (whether it is memcpy from a string literal or loading a byte at a time with the instructions for the built-in storage or in some other way) is completely determined by the implementation. Note that initialization must be performed every time a variable falls into scope, so if it does not change, it is a very wasteful construct to use.

If it is a static / global variable, it will create a 6-byte char array with a unique address / storage, the original contents of which are the given characters and which is writable.

Further:

 char *b="asdf"; 

This initializes the pointer b to point to the string literal "asdf" , which may or may not share storage with other string literals and which creates undefined behavior if you write to it.

+2
source

Both parameters [] = "aesdf" and char a [] = {'a', 'e', ​​'s', 'd', 'f', '\ 0'} will be stored in the function start time and memory will be freed when function returns. but for char * b = "asdf", asdf is stored in the readonly section and sent from there.

0
source
 char a[] = "aesdf"; char a[] = {'a','e','s','d','f','\0'}; 

These two lines of code have the same effect. The compiler can choose to implement them the same way, otherwise it can choose them differently.

From the point of view of a programmer writing code in C, this does not really matter. You can use them and make sure that you end up with a six-element array of char initialized with the specified content.

Will "aesdf" exist for the entire duration of the executable file?

Semantically, yes. String literals are char arrays that have a static storage duration. An object with static memory has a program lifespan: it is initialized before the program starts and exists until the program terminates.

However, this does not make any difference in your program. This string literal is used to initialize array a . Since you are not getting a pointer to the actual string literal, it does not matter what its actual lifetime is for this string literal or how it is actually stored. The compiler can do whatever it sees fit as long as array a is properly initialized.

0
source

All Articles