Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where will a constant string be stored in memory?

Tags:

c

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 ?

like image 218
Jeegar Patel Avatar asked Oct 30 '11 06:10

Jeegar Patel


2 Answers

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.

like image 95
cnicutar Avatar answered Nov 20 '22 01:11

cnicutar


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)

like image 39
Mysticial Avatar answered Nov 20 '22 01:11

Mysticial