=proto
void *duk_alloc(duk_context *ctx, size_t size);
=summary
Like duk_alloc_raw()
but may trigger
a garbage collection to satisfy the request. However, the allocated memory
itself is not automatically garbage collected. The allocation request may
fail even after garbage collection, in which case a NULL
is returned.
Memory allocated with duk_alloc()
can be freed with either
duk_free()
or
duk_free_raw()
.
=example
/* Although duk_alloc() triggers a GC if necessary, it can still fail to
* allocate the desired amount of memory. Caller must check for NULL
* (however, if allocation size is 0, a NULL may be returned even in
* a success case).
*/
void *buf = duk_alloc(ctx, 1024);
if (buf) {
printf("allocation successful: %p\n", buf);
} else {
printf("allocation failed\n");
}
=tags
memory
=seealso
duk_alloc_raw
=fixme
Zero allocated memory automatically?