Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>No, the code should not (necessarily) give a segfault. A segfault occurs when you attempt to access a virtual memory page that is not allocated to your process.</p> <p>The "heap" or "free store" is a region of virtual memory pages owned by your process. The <code>malloc()</code> API sub-divides this region into blocks and returns a pointer to the block.</p> <p>If you address beyond the end of the block to which you have a pointer, you will usually access memory that is part of the heap, but not part of your allocated block. In this way, you can corrupt other heap blocks or even the data structures which <code>malloc()</code> uses to define the heap.</p> <p>For more information on heap corruption, and methods to detect it in the debug version of your code, this is a great book:</p> <p><a href="https://rads.stackoverflow.com/amzn/click/1556155514" rel="nofollow noreferrer">Writing Solid Code: Microsoft's Techniques for Developing Bug-Free C Programs by Steve Maguire</a> <a href="https://rads.stackoverflow.com/amzn/click/1556155514" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vyYUx.jpg" alt="alt text"></a></p> <p>An addendum for the pedantic: In rare cases, by accessing memory beyond the end of a heap block, you may access memory that is not part of the heap. In these cases, you may get the segmentation fault you expected. You might also corrupt some other data structure than the heap. It's really a matter of chance. However, the heap itself is very large compared to typical heap blocks, so 99% of the time code such as your example will corrupt the heap. The example you provide falls into that 99% case.</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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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