Possible Duplicate:
C String literals: Where do they go?
Here is a piece of C code that I was asked to analyze in an interview.
int main() {
char *ptr = "hello";
return 0;
}
Which part of the memory does the string “hello” get stored?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
This is implementation-specific and not specified by the standard. You’d have to consult the documentation for your particular compiler to determine where it’s placed.
Generally, compilers place string literals in a read-only data segment such as the code segment. This allows multiple different string literals to be encoded in the program using a single piece of memory, which can be shared. It’s also why it’s a Bad Idea to try to modify a string literal in-place, since this often triggers a segmentation fault due to writing to a read-only segment. This isn’t guaranteed, but it’s often implemented this way.