简体   繁体   中英

Difference between Array initializations

Please see the following statements:

char a[5]="jgkl"; // let's call this Statement A
char *b="jhdfjnfnsfnnkjdf"; // let's call this Statement B , and yes i know this is not an Array
char c[5]={'j','g','k','l','\0'}; // let's call this Statement C

Now, is there any difference between Statements A and C? I mean both should be on Stack dont they? Only b will be at Static location.


So wouldn't that make "jgkl" exist at the static location for the entire life of the program? Since it is supposed to be read-only/constant? Please clarify.

No, because the characters "jgkl" from Statement A are used to initialize a , it does not create storage in the executable for a character string (other than the storage you created by declaring a ). This declaration creates an array of characters in read-write memory which contain the bytes {'j','g','k','l','\\0'} , but the string which was used to initialize it is otherwise not present in the executable result.

In Statement B, the string literal's address is used as an initializer. The variable char *b is a pointer stored in read-write memory. It points to the character string "jhdfjnfnsfnnkjdf" . This string is present in your executable image in a segment often called ".sdata", meaning "static data." The string is usually stored in read-only memory, as allowed by the C standard.

That is one key difference between declaring an array of characters and a string constant: Even if you have a pointer to the string constant, you should not modify the contents.

Attempting to modify the string constant is "undefined behavior" according to ANSI C standard section 6.5.7 on initialization.

If a[] is static then so is c[] - the two are equivalent, and neither is a string literal. The two could equally well be declared so that they were on the stack - it depends where and how they are declared, not the syntax used to specify their contents.

the value "jgkl" may never be loaded into working memory. Before main is ever called, a function (often called cinit ) is run. One of the things this function does is initialize static and file-scope variables. On the DSP compiler I use, the initial values are stored in a table which is part of the program image. The table's format is unrelated to the format of the variables that are being initialized. The initializer table remains part of the program image and is never copied to RAM. Simply put, there is nowhere in memory that I can reliably access "jgkl".

Small strings like a might not even be stored in that table at all. The optimizer may reduce that to the equivalent (pseudoinstruction) store reg const(152<<24|167<<16|153<<8|154)

I suspect most compilers are similar.

A and C are exactly equivalent. The syntax used in A is an abbreviation for the syntax in C.

Each of the objects named a and c is an array of bytes of length 5, stored at a certain location in memory which is fixed during execution. The program can change the element bytes at any time. It is the compiler's responsibility to decide how to initialize the objects. The compiler might generate something similar to a[0] = 'j'; a[1] = 'g'; ... a[0] = 'j'; a[1] = 'g'; ... a[0] = 'j'; a[1] = 'g'; ... , or something similar to memcpy(a, static_read_only_initialization_data[1729], 5) , or whatever it chooses. The array is on the (conceptual) stack if the declaration occurs in a function, or in global writable memory if the declaration occurs at file scope.

The object named b is a pointer to a byte. Its initial value is a pointer to string literal memory, which is read-only on many implementations that have read-only memory, but not all. The value of b could change (for example to point to a different string, or to NULL ). The program is not allowed to change the contents of jhdfjnfnsfnnkjdf" , though as usual in C the implementation may not enforce this.

C-Literals always are read-only.

  • a) allocates 5 bytes memory and store the CONTENT from literal incl. '\\0' in it
  • b) allocates sizeof(size_t) bytes memory and store the literal-address in it
  • c) allocates 5 bytes memory and store the 5 character-values in it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM