![]() |
![]()
| ![]() |
![]()
LIBRARY#include <xtend/mem.h> -lxtend SYNOPSISvoid *xt_malloc(size_t nelem, size_t size) ARGUMENTSnelem: Number of objects to allocate size: Size of a single object DESCRIPTIONxt_malloc() is a simple wrapper around malloc(3) that requires two arguments representing the number of objects to allocate and the size of an element. This prevents the very common mistake with malloc(3) of forgetting to multiply by the size of an element. Specifying the size using sizeof(*variable) has the advantage of being type-independent. I.e. if you change the type of the variable, this code need not be updated. Simply add one * to whatever the return value is assigned to. RETURN VALUESAddress of the newly allocated array, or NULL if allocation failed EXAMPLESsize_t widget_list_size = 1024; widget_t *widgets; widgets = xt_malloc(widget_list_size, sizeof(*widgets)); SEE ALSOmalloc(3)
|