char * variable address vs. char [] variable address
I am printing out 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 printed, the following output occurs with wildly different addresses for the variables charArray and strPtr. The question is, "Why?"
Printing:
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
The different addresses, as you see, are: 0x400880 vs. 0x7fff12d5ed30
The rest of the variable declared before this 开发者_开发技巧have addresses like that of charArray.
Again, the question is, "Why are the addresses so different?" Thanks for any assistance.
Because string literals, e.g. "foo bar" get allocated in a "different place" than your char array.
This is implementation dependent, but a typical implementation will put string literals in your .rdata
("read-only data") section of your executable, and your char array is declared locally, and hence goes on the stack.
And different sections of your image will get mapped to vastly different addresses when they get loaded in RAM.
I am guessing the compiler/linker puts the char array on the stack, whereas the the other string is put into a static string table.
The text "Chars in a char array variable." and "This is a string, made on the fly." are probably quite near each other. However, char charArray[] = ...
requests space on the stack into which the corresponding bit of text is copied. The stack is practically in a different universe from the original hard-coded text, once the OS is done with its virtualization etc.
This is how it goes - I remember reading about this in [Unix: Systems Programming]
1
As you can see Initialized static data gets stored in a different location on the heap as opposed to uninitialized static data.
The crucial thing to realise here is that in the case of strPtr
, you are dealing with two different objects, whereas in the case of charArray
, you are dealing with only one.
charArray
is a single array object, filled with the characters of the "Chars in a char array variable."
string.
strPtr
itself is a single pointer object. Its value is the address of a second anonymous, unmodifiable array object that in turn contains the characters of the "This is a string, made on the fly."
string.
When you print out charArray
using %p
, you are printing the address of charArray[0]
(due to a special rule for arrays). When you print out &(*strPtr)
(which is exactly the same as just strPtr
), you are printing the address of the anonymous, unmodifiable array object mentioned earlier - and this is why it appears so different to the addresses of the other variables involved.
If you print out &strPtr
using %p
, you will see that the address of the variable strPtr
itself is in a similar range to the other local variables.
精彩评论