name: duk_push_buffer proto: | void *duk_push_buffer(duk_context *ctx, duk_size_t size, duk_bool_t dynamic); stack: | [ ... ] -> [ ... buf! ] summary: |

Allocate a new buffer of size bytes and push it to the value stack. Returns a non-NULL pointer to the buffer data area; for a zero-size buffer, may return either NULL or non-NULL. The buffer data area is automatically zeroed. If dynamic is non-zero, the buffer will be resizable, otherwise the buffer will have a fixed size. Throws an error if allocation fails.

There are also shortcut variants duk_push_fixed_buffer() and duk_push_dynamic_buffer().

A dynamic buffer requires two memory allocations internally: one for the buffer header and another for the currently allocated data area. A fixed buffer only requires a single allocation: the data area follows the buffer header.
Be careful when requesting a zero length dynamic buffer: a NULL data pointer is not an error and should not confuse calling code.
Duktape can be compiled with a feature option which disables automatic zeroing of allocated buffer data. If this is the case, you need to zero the buffer manually if necessary.
example: | /* Allocate a fixed buffer of 1024 bytes. There is no need to check for * the return value: an error is thrown if allocation fails. */ void *p; p = duk_push_buffer(ctx, 1024, 0); printf("allocated buffer, data area: %p\n", p); tags: - stack - buffer seealso: - duk_push_fixed_buffer - duk_push_dynamic_buffer - duk_push_external_buffer introduced: 1.0.0