Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>For, </p> <blockquote> <blockquote> <p>char a[]="June 14";</p> </blockquote> </blockquote> <p>gcc on Linux allocates memory in the stack area. Following transformations happens,</p> <blockquote> <blockquote> <ol> <li>allocate memory for a in stack for size of ("June 14") + 1 </li> <li>Then copies the "June 14" into the memory area when we call the function</li> </ol> </blockquote> </blockquote> <p>For,</p> <blockquote> <blockquote> <p>char *b="June 14";</p> </blockquote> </blockquote> <p>gcc on Linux allocates the memory in the .rodata and stores the "June 14" string in the .rodata section. Then variable b is allocated and it holds the address of the memory area of "June 14" string which is in the .rodata section.</p> <p>Look at the sample C program for the same,</p> <pre><code> #include&lt;stdio.h&gt; main() { char a[]="Test Message"; //memory is allocated in the stack and stores "Test Message" char *b="Test Const String"; //Memory allocated in .rodata section and b points to the memory address } </code></pre> <p>I have compiled the above code with gcc with -c options and analyzed the obj file using objdump command. Following is the output, constPointer.o: file format elf32-i386</p> <pre><code> Contents of section .text: 0000 8d4c2404 83e4f0ff 71fc5589 e55183ec .L$.....q.U..Q.. **0010 24c745eb 54657374 c745ef20 4d6573c7 $.E.Test.E. Mes. 0020 45f37361 6765c645 f700c745 f8000000 E.sage.E...E....** 0030 0083c424 595d8d61 fcc3 ...$Y].a.. Contents of section .rodata: **0000 54657374 20436f6e 73742053 7472696e Test Const Strin 0010 6700 g.** Contents of section .comment: 0000 00474343 3a202847 4e552920 342e332e .GCC: (GNU) 4.3. 0010 30203230 30383034 32382028 52656420 0 20080428 (Red 0020 48617420 342e332e 302d3829 00 Hat 4.3.0-8). </code></pre> <p>I have highlighted(using **) memory allocation done in the different section. In that "Test const String" is placed in the .rodata section which is not modifiable.</p> <p>So, a[3]='t'; will compile without any error or warning, but b[2]='t'; will result in run time error.</p> <p>Hope this helps. </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. 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