HiveBrain v1.2.0
Get Started
← Back to all entries
patterncMinor

Are these memory-allocation wrapper functions kosher with all C compilers?

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
thesecompilersallarewithwrappermemorykosherfunctionsallocation

Problem

I have never been entirely comfortable using malloc() and free() directly. For one thing, 99% of the time that I would like to call malloc, I would prefer it to take two arguments like calloc and do the multiplication itself, instead of taking one argument. Beyond that, I've often wished that free could magically zero out the pointer it is passed (which, of course, is impossible for it to do, since its pointer argument is pass-by-value).

So I've put together a very small library of wrapper functions to address these and minor other issues I have with malloc / calloc / realloc / free / memset / memcpy. The functions in this little library are:

  • mem_alloc — Allocate a block of memory, given an item count and item size. This is simply a malloc wrapper.



  • mem_alloc_clear — Allocate and clear a block of memory, given an item count and item size. This is simply a calloc wrapper.



  • mem_clear — Clear a block of memory, given a pointer, item count, and item size. This is a memset / bzero wrapper.



  • mem_copy — Copy a block of memory, given a source and destination pointer, item count, and item size. This is a memcpy wrapper.



  • mem_realloc — Reallocate a block of memory, given a pointer, item count, and item size. This is simply a realloc wrapper.



  • mem_alloc_copy — Allocate a block of memory and copy memory into it, given a source pointer, item count, and item size.



  • mem_alloc_clone — Allocate a block of memory and copy memory into it, given only a source pointer. The size of the block is automagically determined by calling mem_size.



  • mem_dealloc — Deallocate a block of memory, given a pointer, and set that pointer to null. This is a free wrapper, implemented as a hidden function and a visible macro.



  • mem_size — Return the size of a previously allocated block of memory. This is simply a malloc_size wrapper.



Note that none of these take a size_t of bytes directly. Instead, they compute the size by multi

Solution

Your code looks good, but your library doesn't seem to do much. The thing that bothers you (from my understanding), is that you can't call malloc like calloc and that the pointer given to free doesn't get set to NULL.

IMHO, that doesn't need a whole library which basically only wraps functions. The following two macros would achive the same, but without the overhead:

#define mymalloc(item_count, item_size) malloc((item_count) * (item_size))
#define myfree(ptr) do { \
  free(ptr); \
  (ptr) = NULL; \
} while(0)


Why the whole library if the two macros above could do the same?

Code Snippets

#define mymalloc(item_count, item_size) malloc((item_count) * (item_size))
#define myfree(ptr) do { \
  free(ptr); \
  (ptr) = NULL; \
} while(0)

Context

StackExchange Code Review Q#26656, answer score: 4

Revisions (0)

No revisions yet.