bmalloc, bcalloc, brealloc, bstrdup, bstrndup

(Checked memory allocation)


#include <tap/basic.h>

void *bmalloc(size_t size);

void *bcalloc(size_t n, size_t size);

void *brealloc(void *ptr, size_t size);

char *bstrdup(const char *string);

char *bstrndup(const char *string, size_t size);


These functions are wrappers around the standard C memory allocation functions. They each check whether the memory allocation succeeded, returning the newly allocated memory on success and calling sysbail() on failure. They're intended for use inside TAP test cases where the test case should be aborted if memory allocation fails, and avoid the need to check each allocation as it's performed.


bmalloc() and bcalloc() return a pointer to the newly allocated memory, and brealloc() a pointer to the reallocated memory (which may or may not be the same as ptr), identical to their standard C counterparts. bstrdup() returns a pointer to a newly allocated copy of string. bstrndup() does the same, but copies at most size characters of string and always nul-terminates the returned string. None of these functions may return NULL; if memory allocation fails, they will abort the program.


bmalloc() and bcalloc() do not guard against allocating 0 bytes, just calling the underlying C library functions, and may return NULL in that case on some platforms.


Russ Allbery <>


Copyright 2011, 2012, 2013 Russ Allbery <>

Copying and distribution of this file, with or without modification, are permitted in any medium without royalty provided the copyright notice and this notice are preserved. This file is offered as-is, without any warranty.


calloc(3), malloc(3), realloc(3), strdup(3), strndup(3), sysbail(3)

The current version of the C TAP Harness library is available from its web page at <>.

Last spun 2016-11-05 from POD modified 2016-10-26