C: Is an Anonymous Array Allocated on the Heap or Stack?

386 views Asked by At

For example this code here:

char *s = "Hello";

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

2

There are 2 answers

2
Vlad from Moscow On BEST ANSWER

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.

3
Grzegorz Szpetkowski On

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.