Aussie AI
Initialize memory blocks with memset
-
Book Excerpt from "Generative AI in C++"
-
by David Spuler, Ph.D.
Initialize memory blocks with memset
The memset
function sets all of a memory block to a byte value.
It is widely used as a fast way to initialize a block of memory
to all zeros.
memset(&x, 0, sizeof(x));
Almost all usages of memset
will be for the zero byte.
The only other usage I've seen is to fill memory with
a dummy non-zero byte as a form of mutation testing
to catch uses of uninitialized memory.
memset(&x, 0x55, sizeof(x));
memset sizeof problem.
Here's a common glitch in using memset
inside functions:
void zero_array(int arr[10]) { memset(&arr, 0, sizeof(arr)); // Bug }
The problem is not memset
, but the sizeof
operator
on function parameters.
An array parameter in a function is like a hologram
and isn't really there.
It's not really an array, but a pointer,
and sizeof(int[10])
is the same as sizeof(int*)
.
Hence, sizeof(arr)
is probably only 4 or 8, rather than 40 or 80,
leaving most of the array uninitialized.
Personally, I recommend a memset
debug wrapper function
to catch this kind of problem at runtime,
or maybe a tricky preprocessor macro can detect it at compile-time
with a static_assert
somehow.
memset portability issue.
Even though it's a fast zeroing method, the use of
memset
to zero bytes has an obscure portability problem
on any architecture where all-bytes-zero is not the
same as all data types zero.
However, on most standard platforms, all-bytes-zero is correct
for all types: integer zero (regardless of endianness),
floating-point zero (positive zero is all bits zero), and the null pointer.
Zero portability is covered in more detail in Chapter 38.
• Next: • Up: Table of Contents |
The new AI programming book by Aussie AI co-founders:
Get your copy from Amazon: Generative AI in C++ |