Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <ol> <li><p>Using either <code>memset(ptr, NULL, size)</code> or <code>memset(ptr, 0xDEADBEEF, size)</code> is a clear indication of the fact that the author did not understand what they were doing.</p> <p>Firstly, <code>memset(ptr, NULL, size)</code> will indeed zero-out a memory block in C and C++ if <code>NULL</code> is defined as an integral zero. </p> <p>However, using <code>NULL</code> to represent the zero value in this context is not an acceptable practice. <code>NULL</code> is a macro introduced specifically for pointer contexts. The second parameter of <code>memset</code> is an integer, not a pointer. The proper way to zero-out a memory block would be <code>memset(ptr, 0, size)</code>. Note: <code>0</code> not <code>NULL</code>. I'd say that even <code>memset(ptr, '\0', size)</code> looks better than <code>memset(ptr, NULL, size)</code>.</p> <p>Moreover, the most recent (at the moment) C++ standard - C++11 - allows defining <code>NULL</code> as <code>nullptr</code>. <code>nullptr</code> value is not implicitly convertible to type <code>int</code>, which means that the above code is not guaranteed to compile in C++11 and later.</p> <p>In C language (and your question is tagged C as well) macro <code>NULL</code> can expand to <code>(void *) 0</code>. Even in C <code>(void *) 0</code> is not implicitly convertible to type <code>int</code>, which means that in general case <code>memset(ptr, NULL, size)</code> is simply invalid code in C.</p> <p>Secondly, even though the second parameter of <code>memset</code> has type <code>int</code>, the function interprets it as an <code>unsigned char</code> value. It means that only one lower byte of the value is used to fill the destination memory block. For this reason <code>memset(ptr, 0xDEADBEEF, size)</code> will compile, but will not fill the target memory region with <code>0xDEADBEEF</code> values, as the author of the code probably naively hoped. <code>memset(ptr, 0xDEADBEEF, size)</code> is eqivalent to <code>memset(ptr, 0xEF, size)</code> (assuming 8-bit chars). While this is probably good enough to fill some memory region with intentional "garbage", things like <code>memset(ptr, NULL, size)</code> or <code>memset(ptr, 0xDEADBEEF, size)</code> still betray the major lack of professionalism on the author's part.</p> <p>Again, as other answer have already noted, the idea here is to fill the unused memory with a "garbage" value. Zero is certainly not a good idea in this case, since it is not "garbagy" enough. When using <code>memset</code> you are limited to one-byte values, like <code>0xAB</code> or <code>0xEF</code>. If this is good enough for your purposes, use <code>memset</code>. If you want a more expressive and unique garbage value, like <code>0xDEDABEEF</code> or <code>0xBAADFOOD</code>, you won't be able to use <code>memset</code> with it. You'll have to write a dedicated function that can fill memory region with 4-byte pattern.</p></li> <li><p>A pointer in C and C++ cannot be assigned an arbitrary integer value (other than a Null Pointer Constant, i.e. zero). Such assignment can only be achieved by forcing the integral value into the pointer with an explicit cast. Formally speaking, the result of such a cast is implementation defined. The resultant value can certainly point to valid data.</p></li> </ol>
    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.
    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