According to the manual, GC_MALLOC
clears the memory but GC_MALLOC_ATOMIC
does not clear the memory.
void * GC_MALLOC(size_t nbytes)
Allocates and *clears* nbytes of storage.
void * GC_MALLOC_ATOMIC(size_t nbytes)
Allocates nbytes of storage.
https://www.hboehm.info/gc/gcinterface.html
So GC_MALLOC_ATOMIC
should be used to replace malloc
and GC_MALLOC
should be used to replace calloc
? Is it so?
No.
You are correct that
GC_MALLOC_ATOMIC()
is likemalloc()
in thatmalloc()
makes no guarantee to clear the allocated space, whereasGC_MALLOC()
is likecalloc()
in that it does clear the allocated space, but in the most general sense, you should replace bothmalloc()
andcalloc()
withGC_MALLOC()
. This is because,GC_MALLOC_ATOMIC()
documents this constraint:On the other hand, one usually has some knowledge about the usage of the space one is allocating, and when one does,
GC_MALLOC_ATOMIC()
is to be preferred for objects that do not contain pointers. This is because GC will not (ever) spend time scanning the resulting objects for pointers. If you want the results zero-filled, then do that manually, afterward.memset()
is a common means to do this.