title: Visual studio 2010 - data segment and stack memory are same
body: <p>I figured out that a constant literal get's placed in the data segment of the program (from SO) and is read-only and hence the line " s[0] = 'a' " would cause an error, which actually did happen when I uncommented that line and ran. However when I looked into the memory window in MS VS, the variables are all placed together in memory. I am curious as to how they(compiler) enforce read-only access to 's'? </p>
  
  <pre><code>#include &lt;iostream&gt;
  
  int main(void)
  {
        char *s = "1023";
        char s_arr[] = "4237";
        char *d = "5067";
        char s_arr_1[] = "9999";
        char *e = "6789";
        printf("%c\n", s[0]);
  //      s[0] = 'a'; This line would error out since s should point to data segment of the program
        printf("%s\n", s);
        system ("pause");
  }
  
  0x002E54F4  31 30 32 33 00 00 00 00 34 32 33 37 00 00 00 00  1023....4237....
  0x002E5504  35 30 36 37 00 00 00 00 39 39 39 39 00 00 00 00  5067....9999....
  0x002E5514  36 37 38 39 00 00 00 00 25 63 0a 00 25 73 0a 00  6789....%c..%s..
  0x002E5524  70 61 75 73 65 00 00 00 00 00 00 00 43 00 3a 00  pause.......C.:.
  </code></pre>
  
  <p>Edit 1:
  Updating the value stored in s_arr (which should be placed in stack space) to make it clear that it is placed adjacent to the string constants. </p>
  
  <p>Edit 2: Since I am seeing answers regarding ro/rw access based on pages,
  Here address .. 0x...4f4 is rw 0x...4fc is <strong>ro</strong> and again 0x...504 is rw. How do they achieve this granularity? Also since the each page could be a minimum of 4kb, one could argue that the 0x4fb could be the last address of the previous ro page. But I have now added a few more variables to show that they are all placed  contiguously in memory and the granularity is per every 8 bytes.
  You could say, Since pages are at 4k level as you mentioned, </p>