Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a const static string be allocated on the stack?

Tags:

const char * foo() {     return "abcdef"; }  int main() {     printf("%s", foo()); } 

Can a conforming compiler decide to allocate "abcdef" on the stack? I.e. what in the standard forces the compiler to allocate it in the .data section?

like image 396
qdii Avatar asked Nov 17 '14 12:11

qdii


People also ask

Is const stored in stack?

'const' variable is stored on stack. 'const' is a compiler directive in "C". The compiler throws error when it comes across a statement changing 'const' variable.

Are strings allocated on the stack?

In the case of strings being initialized via string literals (ie: "stack" ), it is allocated in a read-only portion of memory. The string itself should not be modified, as it will be stored in a read-only portion of memory. The pointer itself can be changed to point to a new location.

Are strings allocated on heap or stack?

Stack space contains specific values that are short-lived whereas Heap space used by Java Runtime to allocate memory to objects and JRE classes. In Java, strings are stored in the heap area. Why Java strings stored in Heap, not in Stack? String Literal is created by using a double quote.


1 Answers

From the C++ specification § 2.14.5/8 for string literals;

Ordinary string literals and UTF-8 string literals are also referred to as narrow string literals. A narrow string literal has type “array of n const char”, where n is the size of the string as defined below, and has static storage duration (3.7).

It is also worthwhile mentioning this, static storage duration, applies to all the string literals; hence L"", u"", U"" etc; § 2.14.5/10-12.

In turn, for the static storage duration § 3.7.1/1;

All variables which do not have dynamic storage duration, do not have thread storage duration, and are not local have static storage duration. The storage for these entities shall last for the duration of the program (3.6.2, 3.6.3).

Hence, your string "abcdef" shall exist for the duration of the program. The compiler can choose where to store it (and this may be a system constraint), but it must remain valid.

For the C language specification (C11 draft n1570), string literals § 6.4.5/6;

In translation phase 7, a byte or code of value zero is appended to each multibyte character sequence that results from a string literal or literals. The multibyte character sequence is then used to initialize an array of static storage duration and length just sufficient to contain the sequence. For character string literals, the array elements have type char, and are initialized with the individual bytes of the multibyte character sequence.

And the static storage duration § 6.2.4/3;

An object whose identifier is declared without the storage-class specifier _Thread_local, and either with external or internal linkage or with the storage-class specifier static, has static storage duration. Its lifetime is the entire execution of the program and its stored value is initialized only once, prior to program startup.

The same rationale for the location applies (it will most likely be a system constraint), but must remain valid for the duration of the program.

like image 174
Niall Avatar answered Nov 08 '22 06:11

Niall