Reference:
- difference between char array[] and char *array
- https://stackoverflow.com/questions/5325326/what-is-the-meaning-of-each-line-of-the-assembly-output-of-a-c-hello-world
- https://stackoverflow.com/questions/10004511/why-are-string-literals-l-value-while-all-other-literals-are-r-value
- https://gcc.gnu.org/onlinedocs/gcc/Compound-Literals.html
quote:
A string literal is a literal with array type, and in C there is no way for an array type to exist in an expression except as an lvalue.
String literals could have been specified to have pointer type (rather than array type that usually decays to a pointer) pointing to the string "contents", but this would make them rather less useful; in particular, the sizeof operator could not be applied to them.
Note that C99 introduced compound literals, which are also lvalues, so having a literal be an lvalue is no longer a special exception; it's closer to being the norm.
quote:
const char hello[] = {'h', 'e', 'l', 'l', 'o', '\0'};
This creates an array of 6 bytes in writable memory (on the stack if this is inside a function, in a data segment if directly at global scope or inside a namespace), to be initialized with the ASCII codes for each of those characters in turn.
quote:
const char hello[] = {'h', 'e', 'l', 'l', 'o', '\0'};
This creates an array of 6 bytes in writable memory (on the stack if this is inside a function, in a data segment if directly at global scope or inside a namespace), to be initialized with the ASCII codes for each of those characters in turn.
i.e 以上可以被take address, 可以用於C++ template non-type parameter.
char *hello = "hello";
此為runtime(其所在記憶體位置由loader決定), 不能被take address at compile time, 故不能用於template non-type parameter.
char *hello = "hello";
此為runtime(其所在記憶體位置由loader決定), 不能被take address at compile time, 故不能用於template non-type parameter.
"hello" is a string literal,
which typically means: the OS loader code that loads your program into memory and starts it running will have copied the text "hello\0" from your executable image into some memory that will then have been set to be read only, and a separate variable named "hello" - which is of whatever size pointers are in your program (e.g. 4 bytes for 32-bit applications, 8 for 64-bit) - will exist on the stack (if the line above appears inside a function) or in writable memory segment (if the line is at global or namespace scope), and the address of the former textual data will be copied into the hello pointer. you can change hello to point somewhere else (e.g. to another language's equivalent text), but normally shouldn't try to change the string literal to which the above code points hello.
Beware not to modify char*, which should be const char*, since we could cache the char string with same value at the beginning.
which typically means: the OS loader code that loads your program into memory and starts it running will have copied the text "hello\0" from your executable image into some memory that will then have been set to be read only, and a separate variable named "hello" - which is of whatever size pointers are in your program (e.g. 4 bytes for 32-bit applications, 8 for 64-bit) - will exist on the stack (if the line above appears inside a function) or in writable memory segment (if the line is at global or namespace scope), and the address of the former textual data will be copied into the hello pointer. you can change hello to point somewhere else (e.g. to another language's equivalent text), but normally shouldn't try to change the string literal to which the above code points hello.
Beware not to modify char*, which should be const char*, since we could cache the char string with same value at the beginning.
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.