# Chapter 6. Memory (de)allocation

## 1. `igraph_malloc` — Allocate 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. `igraph_calloc` — Allocate 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()`

## 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()`

## 4. `igraph_free` — Deallocate 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()`