-2

For example this code here:

char *s = "Hello";

Where is "Hello" being stored? Is it stored the same in memory just anonymously?

SirIrk
  • 23
  • 1

2 Answers2

2

String literals have static storage duration and are allocated in the static memory that is neither on the stack nor in the heap. For example they can be allocated in a read only data segment.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
2

C Standard does not define, where string literals are stored (it does not even use terms like stack nor heap). It only tells that it has static storage duration. Typically it means that it's located in heap the data segment.

Grzegorz Szpetkowski
  • 36,988
  • 6
  • 90
  • 137
  • 3
    There is no reason to down-vote this answer! The C-language standard indeed doesn't define where variables should be allocated (as far as I know, not only string literals, but any other type of variables as well). It is generally subjected to compiler implementation. You could add the notion that in practice, most decent compilers allocate string literals in the RO-data section of the executable image. But somebody is most definitely playing around with the votes here. – barak manos Nov 16 '14 at 21:21
  • 2
    @barakmanos: Grzegorz is correct that the standard doesn't say where string literals are stored. But they are not typically located on the heap. The heap is the region of memory managed by `malloc` and `free`. The stack, the heap, and the static data area are typically three distinct regions. – Keith Thompson Nov 16 '14 at 21:32
  • @KeithThompson: Oh yeah, I see that part of the answer now... I guess I read only the first sentence (BTW, this can be inferred from the part in my comment about string literals typically located in the RO-data section). – barak manos Nov 16 '14 at 22:04