---
name: "array_allocate"
section: "3diet"
platform: "linux"
package: "libowfat-dev"
version: "0.28-2"
release: "Debian 6.0 (Squeeze)"
platforms: ["linux"]
url: "https://new.carta.tech/array_allocate/3diet"
---

# array\_allocate(3diet)

make sure array has at least n elements allocated

## NAME

array\_allocate - make sure array has at least n elements allocated

## SYNTAX

**#include \<array.h\>**

void\* **array\_allocate**(array\* *x*, uint64 *membersize*, int64 *pos*);

  array *x*;\
   int64 *pos*;\
   *t*\* p = array\_allocate(&*x*,sizeof(*t*),*pos*);

## DESCRIPTION

array\_allocate makes sure that enough bytes are allocated in *x* for at least *pos*+1 objects of type *t*. (The size of *t* must be positive; otherwise the effects are undefined.) If not enough bytes are allocated (or *x* is unallocated), array\_allocate allocates more bytes, moving the dynamically allocated region if necessary. array\_allocate often allocates somewhat more bytes than necessary, to save time later.

array\_allocate then makes sure that the number of bytes initialized covers at least those *pos*+1 objects. If not enough bytes are initialized, array\_allocate initializes more bytes (setting them to 0), up to exactly the end of the *pos*+1st object.

array\_allocate then returns a pointer to the *pos*+1st object; i.e., object number *pos*, with objects numbered starting at 0. This pointer can be used to change or inspect the object. The pointer can continue to be used through subsequent calls to array\_get, array\_start, array\_length, and array\_bytes, but it must not be used after any other operations on this array.

If something goes wrong, array\_allocate returns 0, setting **errno** appropriately, without touching *x*. In particular, array\_allocate returns 0 if

- •

  *x* has failed, or

- •

  *pos* is negative, or

- •

  not enough memory is available.

array\_allocate does *not* change *x* to have failed; if you want to do that, use array\_fail.

## SEE ALSO

[array\_get(3)](/array_get/3.md), [array\_start(3)](/array_start/3.md), [array\_fail(3)](/array_fail/3.md)
