2 ms·
You can do stack allocation in C without "alloca()", e.g. size_t example(const char in, const size_t in_size, char out, const size_t max_out_size) { size_t
by faragon 10y ago
You can do stack allocation in C without "alloca()", e.g.
size_t example(const char in, const size_t in_size, char out, const size_t max_out_size)
{
size_t out_size = 0;
char stack_buf[32768];
char *buf = stack_buf;
char *heap_buf = NULL;
size_t req_size = in_size * 2;
if (req_size < sizeof(stack_buf)) {
buf = heap_buf = (char *)malloc(req_size);
}
// Do stuff
if (heap_buf) {
free(heap_buf);
}
return out_size;
}
- vertex-four 10y agoThat's precisely what's being done in curl. Generally when people say "stack allocation" as an alternative to "malloc" they mean alloca/VLA, so you'll forgive my confusion.
- faragon 10y agoERRATA: if (req_size < sizeof(stack_buf)) Should be: if (req_size > sizeof(stack_buf))