Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>c1 is a local pointer, so it should be on the stack;</p> </blockquote> <p>Correct. Though it could also be allocated in a CPU register. </p> <blockquote> <p>c2 is a static pointer, it should be on the heap;</p> </blockquote> <p>No. A variable declared as static, or a variable declared as global ("file scope") all have something called <em>static storage duration</em>. This means that they must be initialized to a known value before the program is called. Unless the programmer initializes them explicitly, the compiler will ensure that they are set to zero.</p> <p>To make static initialization easier, all such objects of static storage duration are typically placed in a separate, dedicated part of the RAM, often called <a href="http://en.wikipedia.org/wiki/.bss" rel="nofollow">.bss</a>. It is unlikely that .bss will be placed on the stack.</p> <blockquote> <p>c3 is on the heap.</p> </blockquote> <p>The actual pointer C3 is on the stack, but the contents it points at are on the heap. The only variables that are on the heap are those you have dynamically allocated with malloc(). Nothing else is there.</p> <blockquote> <p>My assumption and graph right?</p> </blockquote> <p>C1 is correct, the pointer points at a rodata string literal which is stored in a read-only section of the memory.</p> <p>C2 is incorrect, since it resides in .bss. It points at the rodata string literal though, as in your graph.</p> <p>C3 is incorrect. When you strcpy() something, you make a hardcopy of the string literal in rodata and store that copy in heap, at the area pointed at by C3. C3 itself sits on the stack.</p> <p><strong>EDIT:</strong> Not sure what this graph is worth, but here you go.</p> <pre><code> stack .bss rodata heap | | | | | | | | | c1 |--------------&gt;| "abc" | | | | | | c2 |---&gt;| "abc" | | | | c3 |-----------------------------&gt;|abc | | | | | | | | | </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload