# Chapter 6. Memory (de)allocation

## 1. About allocation functions

Some igraph functions return a pointer vector (igraph_vector_ptr_t) containing pointers to other igraph or other data types. These data types are dynamically allocated and have to be deallocated manually when the user does not need them any more. igraph_vector_ptr_t has functions to deallocate the contained pointers on its own, but in this case it has to be ensured that these pointers are allocated by a function that corresponds to the deallocator function that igraph uses.

To this end, igraph exports the memory allocation functions that are used internally so the user of the library can ensure that the proper functions are used when pointers are moved between the code written by the user and the code of the igraph library.

Additionally, the memory allocator functions used by igraph work around the quirks of classical `malloc`(), `realloc`() and `calloc`() implementations where the behaviour of allocating zero bytes is undefined. igraph allocator functions will always allocate at least one byte.

## 2. Available allocation functions

### 2.1. `igraph_malloc` — Allocates memory that can be safely deallocated by igraph functions.

    void *igraph_malloc(size_t size);

This function behaves like `malloc`(), but it ensures that at least one byte is allocated even when the caller asks for zero bytes.

Arguments:

`size`: | Number of bytes to be allocated. Zero is treated as one byte.

Returns:

`` | Pointer to the piece of allocated memory; `NULL` if the allocation failed.

See also:

`` | `igraph_calloc()`, `igraph_realloc()`, `igraph_free()`

### 2.2. `igraph_calloc` — Allocates memory that can be safely deallocated by igraph functions.

    void *igraph_calloc(size_t count, size_t size);

This function behaves like `calloc`(), but it ensures that at least one byte is allocated even when the caller asks for zero bytes.

Arguments:

`count`: | Number of items to be allocated.
`size`: | Size of a single item to be allocated.

Returns:

`` | Pointer to the piece of allocated memory; `NULL` if the allocation failed.

See also:

`` | `igraph_malloc()`, `igraph_realloc()`, `igraph_free()`

### 2.3. `igraph_realloc` — Reallocate memory that can be safely deallocated by igraph functions.

    void *igraph_realloc(void *ptr, size_t size);

This function behaves like `realloc`(), but it ensures that at least one byte is allocated even when the caller asks for zero bytes.

Arguments:

`ptr`: | The pointer to reallocate.
`size`: | Number of bytes to be allocated.

Returns:

`` | Pointer to the piece of allocated memory; `NULL` if the allocation failed.

See also:

`` | `igraph_free()`, `igraph_malloc()`

### 2.4. `igraph_free` — Deallocates memory that was allocated by igraph functions.

    void igraph_free(void *ptr);

This function exposes the `free`() function used internally by igraph.

Arguments:

`ptr`: | Pointer to the piece of memory to be deallocated.

Time complexity: platform dependent, ideally it should be O(1).

See also:

`` | `igraph_calloc()`, `igraph_malloc()`, `igraph_realloc()`