Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <ul> <li>Why is string stored in the read-write area when it was declared with the const modifier? </li> </ul> <p>That is where all literal constants go. That way they can be shared among processes and among other pointers to them.</p> <ul> <li>Why is "hello world" stored in the read-write area when strings are supposed to be immutable?</li> </ul> <p>Pointer (to literal constant) itself <strong>is</strong> mutable. "hello world" is likely to be stored in global consts.</p> <ul> <li>Is there a way to determine (in the code itself) in what segment and area a variable is stored (C99)?</li> </ul> <p>Globals and statics go to 'globals', local vars and args go to 'stack', <code>malloc</code>'ed go to 'heap'. As for globals, those that are consts may or may not go to 'global consts'.</p> <ul> <li>Why is there not a separation between read-only and read-write areas in the BSS/initialized data segment? </li> </ul> <p>There are <em>usually</em> two segments - one for const data (and that one is shared between processes) and one for mutable data (which cannot be obviously shared).</p> <ul> <li>Would the quoted statement above still be true if the const modifier was left off? If no, why not?</li> </ul> <p>In C++ all string literal are <code>const char*</code>, while in C they are <code>char*</code> by nature. So, there is no problem to add/remove const from <code>string</code> variable in C (apart from warning sometimes), while it will cause const-cast error in C++. Having <code>const</code> in <code>const char* string</code> prohibits <code>string</code>'s data from being modified.</p> <p>P.S. The following code might help you to understand what is going on:</p> <pre><code>int main() { char s1[] = "abc"; char* s2 = "abc"; const char* s3 = "abc"; s1[0] = 'z'; s2[0] = 'x'; // s3[0] = 'y'; will not compile due to const printf("%s %s %s\n", s1, s2, s3); s2++; s3++; // it perfectly fine to modify pointers themselves } </code></pre> <p>Depending on platform, the code might crash (Linux) at s2[0] assignment, or might succeed; in later case, s3 might get modified too (Cygwin) or might not.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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