Char * variable address compared to char [] variable address

I print addresses and strings from the following two declarations and initializations:

char * strPtr = (char *) "This is a string, made on the fly."; char charArray [] = "Chars in a char array variable."; 

When printing, the following output is output with completely different addresses for the variables charArray and strPtr. Question: "Why?"

Print

  printf( "%10s%40s%20p\n", "strPtr", strPtr, &(*strPtr)); printf( "%10s%40s%20p\n", "charArray", charArray, charArray); 

Output:

  strPtr This is a string, made on the fly. 0x400880 charArray Chars in a char array variable. 0x7fff12d5ed30 

Different addresses, as you can see: 0x400880 vs 0x7fff12d5ed30

The rest of the variable declared before this has addresses such as charArray.

Again, the question arises: "Why are the addresses different?" Thanks for any help.

+2
source share
5 answers

Because string literals, for example. "foo bar" stands out in a "different place" than your char array.

It depends on the implementation, but a typical implementation puts string literals in your .rdata (read-only) section of your executable, and your char array is declared locally and therefore goes onto the stack.

And other sections of your image will be displayed at completely different addresses when they are loaded into RAM.

+4
source

I assume that the compiler / linker pushes the char array onto the stack, and the other line is pushed into the static string table.

+3
source

The text "Chars in a char array variable". and โ€œThis is a line made on the fly.โ€ They are probably very close to each other. However, char charArray[] = ... requests the space on the stack into which the corresponding bit of text is copied. The stack is actually located in different universes from the source text with hard coding, as soon as the OS is executed with its virtualization, etc.

+2
source

This is how it happens - I remember reading about it in [ Unix: Systems Programming ]

one alt text

As you can see, initialized static data is stored elsewhere on the heap as opposed to uninitialized static data.

+2
source

Crucial to the implementation here is that in the case of strPtr you are dealing with two different objects, while in the case of charArray you are dealing with only one.

charArray is the only array object filled with the characters of the string "Chars in a char array variable." .

strPtr itself is the only pointer object. Its value is the address of the second anonymous, non-modifiable array, which, in turn, contains the characters of the string "This is a string, made on the fly." .

When you print charArray using %p , you print the address charArray[0] (due to the special rule for arrays). When you print &(*strPtr) (this is exactly the same as strPtr only), you print the address of the anonymous, non-modifiable array object mentioned earlier - and therefore it looks as strong as the addresses of the other variables involved.

If you print &strPtr using %p , you will see that the address of strPtr is in a similar range with other local variables.

+1
source

All Articles