Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I can answer to questions #2 and #3.</p> <p><strong>Answer #2</strong></p> <p>When in C you use pointers you are really using a numerical value that is interpreted as address to memory (logical address on modern OS, see footnotes). You can modify this address at your will. If the value points to an address that is not in your address space you have your segmentation fault.</p> <p>Consider for instance this scenario: your OS gives to your process the address range from 0x01000 to 0x09000. Then</p> <pre><code>int * ptr = 0x01000; printf("%d", ptr[0]); // * prints 4 bytes (sizeof(int) bytes) of your address space int * ptr = 0x09100; printf("%d", ptr[0]); // * You are accessing out of your space: segfault </code></pre> <p>Mostly the causes of segfault, as you pointed out, are the use of pointers to NULL (that is mostly 0x00 address, but implementation dependent) or the use of corrupted addresses.</p> <p>Note that, on linux i386, base and limit register are not used as you may think. They are not per-process limits but they point to two kind of segments: user space or kernel space.</p> <p><strong>Answer #3</strong></p> <p>The stack growth is hardware dependent and not OS dependent. On i386 assembly instruction like push and pop make the stack grow downwards with regard to stack related registers. For instance the stack pointer automatically decreases when you do a push, and increases when you do a pop. OS cannot deal with it.</p> <p><strong>Footnotes</strong></p> <p>In a modern OS, a process uses the so called logic address. This address is mapped with physical address by the OS. To have a note of this compile yourself this simply program:</p> <pre><code>#include &lt;stdio.h&gt; int main() { int a = 10; printf("%p\n", &amp;a); return 0; } </code></pre> <p>If you run this program multiple times (even simultaneously) you would see, even for different instances, the same address printed out. Of course this is not the real memory address, but it is a logical address that will be mapped to physical address when needed.</p>
    singulars
    1. This table or related slice is empty.
    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