size_t
. Moreover, a few bytes might be allocated extra just in front of the last magic number in order to word align this number.new
and malloc
etc. return a pointer just beyond this size field, see figure 1.
Figure 1. Memory layout of an allocated memory block with CWDEBUG_MAGIC
set.
The magic numbers are checked when the memory is freed again. Libcwd uses twelve different magic numbers for six pairs, existing each of a start magic number and an end magic number. The six pairs consist of two groups: for memory allocated by the user and for memory allocated internally by libcwd as part of the debug malloc code itself. Thus, each group consists of three pairs: for memory allocated with malloc
, operator new
and operator new[]
respectively.For example, the following code snippet:
MALLOC : operator new[] (size = 16) = 0x8116460
MALLOC : Allocated memory: 16 bytes in 1 blocks. new[] 0x8116460 magic_tst.cc:174 int [4]; (sz = 16) Test array
MALLOC : delete[] 0x8116460 magic_tst.cc:174 int [4]; (sz = 16) Test array <unfinished> COREDUMP: delete[]: magic number corrupt! Quit (core dumped)