9

sometimes we use this type of code in our c programming.

char *p = "Sam";

Here the address of constant character string "Sam" is going to be stored in char pointer p. now here
i want to ask where the Sam is going to be stored ?

2

2 Answers 2

10

The string "Sam" will usually be stored in global memory in the same region as the global constants.

However, if you did this:

char p[] = "Sam";

Then it would be on the stack instead. (as an array initializer)

Sign up to request clarification or add additional context in comments.

Comments

9

The standard doesn't specify this. Typically the string literal ("Sam") will be stored in the data section, in a read-only page.

As for p itself, it depends on whether it is automatic or static.

5 Comments

where automatic and global are stored?
@Mr.32 The automatic variables are stored on the stack. The "global" or static variables are stored in data (or maybe bss).
On which architecture are there read-only pages to a data section? On Linux/ELF, string literals get stored with other read-only non-code data in the rodata section, which is in the same segment as text.
@DietrichEpp I think on x86 any page can be mapped read-only ?
@cnicutar: protections are set per section on ELF systems (technically, per program-header/segment, the .text and .rodata sections will tipically share a single PT_LOAD program header, see the output of readelf -l).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.