Where is memory allocated using the const char pointer?

Possible duplicate:
Is a string literal in C ++ created in static memory?
C ++ type data storage

In this code:

const char * str = "hello world";

If I understand correctly, the pointer has 4 or 8 bytes, which, I think, will be allocated on the stack. But where is the memory and the memory of "hello world" distributed and stored?
Or what does str mean exactly?

+4
source share
3 answers

It is not highlighted. It is usually stored in a code segment or on the stack. This is up to the compiler. In any case, this indicates a zero-terminated array of characters.

+14
source

Essentailly, which compiled as if you wrote:

 const static char helloworld[12] = {'h', 'e', 'l', 'l', 'o',' ','w', 'o', 'r', 'l', 'd', '\0'}; const char * str = helloworld; 

An array is usually placed in some read-only memory section, probably next to the executable code.

Depending on where it is defined, str will be on the stack or global memory space.

+6
source

C has no stack or heap. C says that "hello world" is a string literal and that string literals have a static storage duration.

+5
source

All Articles