Skip to content

Instantly share code, notes, and snippets.

@begriffs
Created February 15, 2021 16:40
Show Gist options
  • Save begriffs/79919d3d9cf7bd7d9c7a2633e7ad1288 to your computer and use it in GitHub Desktop.
Save begriffs/79919d3d9cf7bd7d9c7a2633e7ad1288 to your computer and use it in GitHub Desktop.
Might be a good idea, might not. Not yet tested
bool nmalloc(size_t n, void ***elts, ...)
{
va_list ap;
*elts = malloc(n * sizeof **elts);
if (!*elts)
return false;
va_start(ap, elts);
for (size_t i = 0; i < n; i++)
{
(*elts)[i] = malloc(va_arg(ap, size_t));
if (!(*elts)[i])
{
for (size_t j = 0; j < i; j++)
free((*elts)[j]);
free(*elts);
return false;
}
}
va_end(ap);
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment