Throughout various code, I have seen memory allocation in debug builds with NULL
...
memset(ptr,NULL,size);
Or with 0xDEADBEEF
...
memset(ptr,0xDEADBEEF,size);
0xDEADBEEF
, couldn't it still deference to valid data? 0xDEADBEEF ("dead beef") is used by IBM RS/6000 systems, Mac OS on 32-bit PowerPC processors and the Commodore Amiga as a magic debug value. On Sun Microsystems' Solaris, it marks freed kernel memory. On OpenVMS running on Alpha processors, DEAD_BEEF can be seen by pressing CTRL-T.
A null pointer is a pointer which points nothing. Some uses of the null pointer are: a) To initialize a pointer variable when that pointer variable isn't assigned any valid memory address yet. b) To pass a null pointer to a function argument when we don't want to pass any valid memory address.
Using either memset(ptr, NULL, size)
or memset(ptr, 0xDEADBEEF, size)
is a clear indication of the fact that the author did not understand what they were doing.
Firstly, memset(ptr, NULL, size)
will indeed zero-out a memory block in C and C++ if NULL
is defined as an integral zero.
However, using NULL
to represent the zero value in this context is not an acceptable practice. NULL
is a macro introduced specifically for pointer contexts. The second parameter of memset
is an integer, not a pointer. The proper way to zero-out a memory block would be memset(ptr, 0, size)
. Note: 0
not NULL
. I'd say that even memset(ptr, '\0', size)
looks better than memset(ptr, NULL, size)
.
Moreover, the most recent (at the moment) C++ standard - C++11 - allows defining NULL
as nullptr
. nullptr
value is not implicitly convertible to type int
, which means that the above code is not guaranteed to compile in C++11 and later.
In C language (and your question is tagged C as well) macro NULL
can expand to (void *) 0
. Even in C (void *) 0
is not implicitly convertible to type int
, which means that in general case memset(ptr, NULL, size)
is simply invalid code in C.
Secondly, even though the second parameter of memset
has type int
, the function interprets it as an unsigned char
value. It means that only one lower byte of the value is used to fill the destination memory block. For this reason memset(ptr, 0xDEADBEEF, size)
will compile, but will not fill the target memory region with 0xDEADBEEF
values, as the author of the code probably naively hoped. memset(ptr, 0xDEADBEEF, size)
is eqivalent to memset(ptr, 0xEF, size)
(assuming 8-bit chars). While this is probably good enough to fill some memory region with intentional "garbage", things like memset(ptr, NULL, size)
or memset(ptr, 0xDEADBEEF, size)
still betray the major lack of professionalism on the author's part.
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 memset
you are limited to one-byte values, like 0xAB
or 0xEF
. If this is good enough for your purposes, use memset
. If you want a more expressive and unique garbage value, like 0xDEDABEEF
or 0xBAADFOOD
, you won't be able to use memset
with it. You'll have to write a dedicated function that can fill memory region with 4-byte pattern.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With