0

For example,

char * first = "Hello World!";

My questions is,

  1. Is the string literal stored in memory? (If it is I guess the pointer first is the address of inital element of the string literal "Hello World")

  2. If not, then is some kind of random pointer value stored in first?

gsamaras
  • 71,951
  • 46
  • 188
  • 305
Jin
  • 1,902
  • 3
  • 15
  • 26

3 Answers3

3

Yes, string literals are stored in memory. The C and C++ standards just say that string literals have static storage duration, any attempt at modifying them gives undefined behavior, and multiple string literals with the same contents may or may not share the same storage.

Shravan40
  • 8,922
  • 6
  • 28
  • 48
3

Yes the string literal is stored in memory, usually it's stored in the .rodata section

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
Ying YIN
  • 73
  • 7
1

It depends on your platform and it is implementation-defined. In general it goes to the read-only memory, if available on your system. Read more here.

Community
  • 1
  • 1
gsamaras
  • 71,951
  • 46
  • 188
  • 305