Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Technically, the standard allows different object pointer types to have different representations (even different sizes), although <code>char*</code> and <code>void*</code> are required have the same representation. But the following is UB:</p> <pre><code>int *ip = 0; free(*(void**)(&amp;ip)); </code></pre> <p>simply because the memory for <code>ip</code> need not be the same size as the memory for a <code>void*</code>, and even if it is the bit pattern for a null pointer of type <code>int*</code> need not be the same as the bit pattern for a null pointer of type <code>void*</code>. If they're different, then of course the compiler has to insert code to convert between them whenever you convert an <code>int*</code> to <code>void*</code> or back.</p> <p>In practice, implementations don't do that to you (and for example Posix forbids it).</p> <p>More importantly though, the strict aliasing rules don't allow you to access a <code>char*</code> object using an lvalue of type <code>void*</code>. So in practice, concerns about pointer representation will not break your code, but the optimizer actually might. Basically, if the function call <code>myfree((void**)(&amp;p))</code> gets inlined, then the compiler might see:</p> <pre><code>char *p = &lt;something&gt;; void **data = (void**)(&amp;p); free(*data); *data = NULL; // code that reads p </code></pre> <p>The optimizer is allowed to note that <code>*data = NULL</code> is setting an object of type <code>void*</code>, whereas the "code that reads p" is reading an object of type <code>char*</code>, which is forbidden from being aliased with that other, <code>void*</code> object over there. So it is allowed to reorder the instructions, eliminate <code>*data = NULL;</code> entirely, or possibly other things I haven't thought of that will ruin your day, but that would speed the code up if you hadn't broken the rules.</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. 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