Like I said in my comment, the details of how things are materialized in memory will vary based on platform. But, here's a very high-level view that should give some flavor of how things work.
First, in the context of the C programming language, objects can have one of several storage durations: static
, automatic
, allocated
, and thread local
. Objects with static
storage duration are allocated as soon as the program starts and released when program exits. Objects with automatic
storage duration are allocated upon entry to their enclosing scope (function or block), and are released as soon as that scope exits. Objects with allocated
storage duration are allocated with a call to malloc/calloc/realloc
, and are released with a call to free
. I'm not going to get into thread local
because it's not really relevant to this discussion.
When your program is loaded into memory, it's laid out something like this (assuming x86 or similar):
+------------------------+
high address | Command line arguments |
| and environment vars |
+------------------------+
| stack | <-- str and her live here, but
| - - - - - - - - - - - | only for the duration of main()
| | |
| V |
| |
| ^ |
| | |
| - - - - - - - - - - - |
| heap |
+------------------------+
| global and read- | <-- "HelloMuraMana" lives here for
| only data | duration of the program
+------------------------+
| program text |
low address | (machine code) |
+------------------------+
The exact picture will depend on your system. Note that this is how things look in the virtual address space, not physical memory.
As your program runs, storage for auto
variables (function arguments and variables local to a function or block like her
and str
) is allocated from the region labeled stack
. Storage for allocated
objects is allocated from the region labeled heap
.
Storage for static
objects as well as storage for string literals like "HelloMuraMana"
is taken from a different segment; in the picture above, it'll be the segment labeled global and read-only data
. Depending on your system, string literals may be stored in a read-only segment (such as .rodata
or .rdata
) or they may be in a writable segment. String literals are supposed to be immutable (hence the term "literal"), so attempting to modify a string literal will result in undefined behavior.
In the layout above, global data objects will have lower addresses than stack or heap objects, which is shown by your output. The variable str
is allocated from the stack when you enter main
; it's value is the address of the string literal, which is allocated from the global data segment when the program first starts.