For using the igraph C library
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.
igraph_malloc
— Allocates memory that can be safely deallocated by igraph functions.igraph_calloc
— Allocates memory that can be safely deallocated by igraph functions.igraph_realloc
— Reallocate memory that can be safely deallocated by igraph functions.igraph_free
— Deallocates memory that was allocated 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:
|
Number of bytes to be allocated. Zero is treated as one byte. |
Returns:
Pointer to the piece of allocated memory; |
See also:
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:
|
Number of items to be allocated. |
|
Size of a single item to be allocated. |
Returns:
Pointer to the piece of allocated memory; |
See also:
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:
|
The pointer to reallocate. |
|
Number of bytes to be allocated. |
Returns:
Pointer to the piece of allocated memory; |
See also:
← Chapter 5. Error handling | Chapter 7. Data structure library: vector, matrix, other data types → |