Note that there are some explanatory texts on larger screens.

plurals
  1. POShould one really set pointers to `NULL` after freeing them?
    primarykey
    data
    text
    <p>There seem to be two arguments why one should set a pointer to <code>NULL</code> after freeing them.</p> <h2><a href="https://stackoverflow.com/questions/1879168/why-set-a-pointer-to-null-after-calling-free-in-c/1879173#1879173">Avoid crashing when double-freeing pointers.</a></h2> <p><em>Short: Calling <code>free()</code> a second time, by accident, doesn't crash when it's set to <code>NULL</code>.</em></p> <ul> <li><p>Almost always this masks a logical bug because there is no reason to call <code>free()</code> a second time. It's safer to let the application crash and be able to fix it.</p></li> <li><p>It's not guaranteed to crash because sometimes new memory is allocated at the same address.</p></li> <li><p>Double free occurs mostly when there are two pointers pointing to the same address.</p></li> </ul> <p>Logical errors can lead to data corruption too.</p> <h2><a href="https://stackoverflow.com/questions/1025589/setting-variable-to-null-after-free/1025604#1025604">Avoid reusing freed pointers</a></h2> <p><em>Short: Accessing freed pointers can cause data corruption if <code>malloc()</code> allocates memory in the same spot unless the freed pointer is set to <code>NULL</code></em></p> <ul> <li><p>There's no guarantee that the program crashes when accessing the <code>NULL</code> pointer, if the offset is big enough (<code>someStruct-&gt;lastMember</code>, <code>theArray[someBigNumber]</code>). Instead of crashing there will be data corruption.</p></li> <li><p>Setting the pointer to <code>NULL</code> cannot solve the problem of having a different pointer with the same pointer value.</p></li> </ul> <h2>The questions</h2> <p>Here's <a href="https://stackoverflow.com/questions/1879168/why-set-a-pointer-to-null-after-calling-free-in-c/1879469#1879469">a post against blindly setting a pointer to <code>NULL</code> after freeing</a>.</p> <ul> <li>Which one is harder to debug?</li> <li>Is there a possibility to catch both?</li> <li>How likely is it, that such bugs lead to data corruption instead of crashing?</li> </ul> <p><em>Feel free to expand this question.</em></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.
 

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