Chapter 6. Memory (de)allocation

1. igraph_malloc — Allocate memory that can be safely deallocated by igraph functions.
2. igraph_free — Deallocate memory that was allocated by igraph functions.

1. igraph_malloc — Allocate memory that can be safely deallocated by igraph functions.

void *igraph_malloc(size_t n);

Some igraph functions, such as igraph_vector_ptr_free_all() and igraph_vector_ptr_destroy_all() can free memory that may have been allocated by the user. igraph_malloc() works exactly like malloc() from the C standard library, but it is guaranteed that it can be safely paired with the free() function used by igraph internally (which is also user-accessible through igraph_free()).

Arguments: 

n:

Number of bytes to be allocated.

Returns: 

Pointer to the piece of allocated memory.

See also: 

2. igraph_free — Deallocate memory that was allocated by igraph functions.

void igraph_free(void *p);

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. This can be done by calling igraph_free on them.

Here is a complete example on how to use igraph_free properly.

Example 6.1.  File examples/simple/igraph_free.c

#include <igraph.h>

int main(void)
{
   igraph_t graph;
   igraph_vector_ptr_t seps;
   long int i;

   igraph_famous(&graph, "tutte");
   igraph_vector_ptr_init(&seps, 0);
   igraph_minimum_size_separators(&graph, &seps);

   for (i=0; i<igraph_vector_ptr_size(&seps); i++) {
     igraph_vector_t *v=VECTOR(seps)[i];
     igraph_vector_print(v);
     igraph_vector_destroy(v);
     igraph_free(v);
   }

   igraph_vector_ptr_destroy(&seps);
   igraph_destroy(&graph);
   return 0;
}


Arguments: 

p:

Pointer to the piece of memory to be deallocated.

Returns: 

Error code, currently always zero, meaning success.

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

See also: