Loading...
Searching...
No Matches
Dynamic Array
libavutil » Memory Management

Utilities to make an array grow when needed. More...

## Functions

void | av_dynarray_add (void *tab_ptr, int *nb_ptr, void *elem)
| Add the pointer to an element to a dynamic array.
av_warn_unused_result int | av_dynarray_add_nofree (void *tab_ptr, int *nb_ptr, void *elem)
| Add an element to a dynamic array.
void * | av_dynarray2_add (void **tab_ptr, int *nb_ptr, size_t elem_size, const uint8_t *elem_data)
| Add an element of size elem_size to a dynamic array.

## Detailed Description

Utilities to make an array grow when needed.

Sometimes, the programmer would want to have an array that can grow when needed. The libavutil dynamic array utilities fill that need.

libavutil supports two systems of appending elements onto a dynamically allocated array, the first one storing the pointer to the value in the array, and the second storing the value directly. In both systems, the caller is responsible for maintaining a variable containing the length of the array, as well as freeing of the array after use.

The first system stores pointers to values in a block of dynamically allocated memory. Since only pointers are stored, the function does not need to know the size of the type. Both av_dynarray_add() and av_dynarray_add_nofree() implement this system.

type **array = NULL; //< an array of pointers to values
int nb = 0; //< a variable to keep track of the length of the array
type to_be_added = ...;
type to_be_added2 = ...;
av_dynarray_add(&array, &nb, &to_be_added);
if (nb == 0)
return AVERROR(ENOMEM);
av_dynarray_add(&array, &nb, &to_be_added2);
if (nb == 0)
return AVERROR(ENOMEM);
// Now:
// nb == 2
// &to_be_added == array[0]
// &to_be_added2 == array[1]
av_freep(&array);
AVERROR
#define AVERROR(e)
Definition error.h:45
av_dynarray_add
void av_dynarray_add(void *tab_ptr, int *nb_ptr, void *elem)
Add the pointer to an element to a dynamic array.
av_freep
void av_freep(void *ptr)
Free a memory block which has been allocated with a function of av_malloc() or av_realloc() family,...

The second system stores the value directly in a block of memory. As a result, the function has to know the size of the type. av_dynarray2_add() implements this mechanism.

type *array = NULL; //< an array of values
int nb = 0; //< a variable to keep track of the length of the array
type to_be_added = ...;
type to_be_added2 = ...;
type *addr = av_dynarray2_add((void **)&array, &nb, sizeof(*array), NULL);
if (!addr)
return AVERROR(ENOMEM);
memcpy(addr, &to_be_added, sizeof(to_be_added));
// Shortcut of the above.
type *addr = av_dynarray2_add((void **)&array, &nb, sizeof(*array),
(const void *)&to_be_added2);
if (!addr)
return AVERROR(ENOMEM);
// Now:
// nb == 2
// to_be_added == array[0]
// to_be_added2 == array[1]
av_freep(&array);
av_dynarray2_add
void * av_dynarray2_add(void **tab_ptr, int *nb_ptr, size_t elem_size, const uint8_t *elem_data)
Add an element of size elem_size to a dynamic array.

## Function Documentation

## av_dynarray_add()

void av_dynarray_add | ( | void * | tab_ptr,
|  | int * | nb_ptr,
|  | void * | elem )

Add the pointer to an element to a dynamic array.

The array to grow is supposed to be an array of pointers to structures, and the element to add must be a pointer to an already allocated structure.

The array is reallocated when its size reaches powers of 2. Therefore, the amortized cost of adding an element is constant.

In case of success, the pointer to the array is updated in order to point to the new grown array, and the number pointed to by nb_ptr is incremented. In case of failure, the array is freed, *tab_ptr is set to NULL and *nb_ptr is set to 0.

Parameters

  [in,out] | tab_ptr | Pointer to the array to grow
  [in,out] | nb_ptr | Pointer to the number of elements in the array
  [in] | elem | Element to add

See also
  av_dynarray_add_nofree(), av_dynarray2_add()

Referenced by av_dynarray_add().

## av_dynarray_add_nofree()

av_warn_unused_result int av_dynarray_add_nofree | ( | void * | tab_ptr,
|  | int * | nb_ptr,
|  | void * | elem )

Add an element to a dynamic array.

Function has the same functionality as av_dynarray_add(), but it doesn't free memory on fails. It returns error code instead and leave current buffer untouched.

Returns
  >=0 on success, negative otherwise

See also
  av_dynarray_add(), av_dynarray2_add()

Referenced by av_dynarray_add_nofree().

## av_dynarray2_add()

void * av_dynarray2_add | ( | void ** | tab_ptr,
|  | int * | nb_ptr,
|  | size_t | elem_size,
|  | const uint8_t * | elem_data )

Add an element of size elem_size to a dynamic array.

The array is reallocated when its number of elements reaches powers of 2. Therefore, the amortized cost of adding an element is constant.

In case of success, the pointer to the array is updated in order to point to the new grown array, and the number pointed to by nb_ptr is incremented. In case of failure, the array is freed, *tab_ptr is set to NULL and *nb_ptr is set to 0.

Parameters

  [in,out] | tab_ptr | Pointer to the array to grow
  [in,out] | nb_ptr | Pointer to the number of elements in the array
  [in] | elem_size | Size in bytes of an element in the array
  [in] | elem_data | Pointer to the data of the element to add. If NULL, the space of the newly added element is allocated but left uninitialized.

Returns
  Pointer to the data of the element to copy in the newly allocated space

See also
  av_dynarray_add(), av_dynarray_add_nofree()

Referenced by av_dynarray2_add().