0
votes

While I know that the memory layout is supposed to be :

Text Segment : Executable instructions(I am guessing machine code in binary)

Initialised Data Segment : Global and static variables that are initialised, if static int a = 10; a is stored here and I am assuming the value 10 is also stored here?

Uninitialised Data Segment : Stores uninitialised variables. static int a; ‘a’ will be stored here.

Stack : Local, temporary variables, recursive function calls, return address.

Heap : Dynamic memory allocated to a variable through malloc or realloc.

char *s = “hello world”

Where will the pointer variable and the string “hello world” be stored?

1
char *s = “hello world” is invalid in C++. It should be const char* s = "hello world;". - Ted Lyngmo
standard doesn't define memory layout. This is compiler platform specific stuff. - Marek R
Where is s being declared? I think most of this isn't specified by the c++ standard and is up to implementations - Alan Birtles
Is there an actual problem to be solved? - n. 1.8e9-where's-my-share m.
@the_minimalist Unfortunately nothing in C++ is const by default (though many of us wish that was not the case) - 0x5453

1 Answers

4
votes

Where is variable data stored in C/C++?

Depends on implementation. Here are some possibilities in general:

  • In memory
  • In a CPU register
  • Nowhere

While I know that the memory layout is supposed to be ...

What you describe may be true of some system / CPU architecture. It is not something specified in the C++ language.


char *s = “hello world”

Where will the pointer variable ... be stored?

Given that the variable has static storage, and it is initialised, if your description is correct, then this applies:

Initialised Data Segment : Global and static variables that are initialised


Where will ... the string “hello world” be stored?

The string literal has static storage and it is initialised. It is not a variable though. None of the descriptions quite fit.


Also, the snippet is ill-formed (since C++11) because string literal is not convertible to pointer to non-const char.