patterncMinor
Are these memory-allocation wrapper functions kosher with all C compilers?
Viewed 0 times
thesecompilersallarewithwrappermemorykosherfunctionsallocation
Problem
I have never been entirely comfortable using
So I've put together a very small library of wrapper functions to address these and minor other issues I have with
Note that none of these take a
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 amallocwrapper.
mem_alloc_clear— Allocate and clear a block of memory, given an item count and item size. This is simply acallocwrapper.
mem_clear— Clear a block of memory, given a pointer, item count, and item size. This is amemset/bzerowrapper.
mem_copy— Copy a block of memory, given a source and destination pointer, item count, and item size. This is amemcpywrapper.
mem_realloc— Reallocate a block of memory, given a pointer, item count, and item size. This is simply areallocwrapper.
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 callingmem_size.
mem_dealloc— Deallocate a block of memory, given a pointer, and set that pointer to null. This is afreewrapper, implemented as a hidden function and a visible macro.
mem_size— Return the size of a previously allocated block of memory. This is simply amalloc_sizewrapper.
Note that none of these take a
size_t of bytes directly. Instead, they compute the size by multiSolution
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
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:
Why the whole library if the two macros above could do the same?
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.