GNU Scientific Library – Reference Manual: Matrix allocation

Next: Accessing matrix elements, Up: Matrices [Index]

#### 8.4.1 Matrix allocation

The functions for allocating memory to a matrix follow the style of `malloc` and `free`. They also perform their own error checking. If there is insufficient memory available to allocate a matrix then the functions call the GSL error handler (with an error number of `GSL_ENOMEM`) in addition to returning a null pointer. Thus if you use the library error handler to abort your program then it isn’t necessary to check every `alloc`.

Function: gsl_matrix * gsl_matrix_alloc (size_t n1, size_t n2)

  This function creates a matrix of size n1 rows by n2 columns, returning a pointer to a newly initialized matrix struct. A new block is allocated for the elements of the matrix, and stored in the block component of the matrix struct. The block is “owned” by the matrix, and will be deallocated when the matrix is deallocated.

Function: gsl_matrix * gsl_matrix_calloc (size_t n1, size_t n2)

  This function allocates memory for a matrix of size n1 rows by n2 columns and initializes all the elements of the matrix to zero.

Function: void gsl_matrix_free (gsl_matrix * m)

  This function frees a previously allocated matrix m. If the matrix was created using `gsl_matrix_alloc` then the block underlying the matrix will also be deallocated. If the matrix has been created from another object then the memory is still owned by that object and will not be deallocated.