Search code examples
gsl

How does a gsl_vector own a gsl_block?


In the reference manual of GSL it is written that

The pointer block stores the location of the memory block in which the vector elements are located (if any). If the vector owns this block then the owner field is set to one and the block will be deallocated when the vector is freed.

But what does it mean 'If the vector owns the block'?

The following is the structure of gsl_vector

typedef struct
{
  size_t size;
  size_t stride;
  double * data;
  gsl_block * block;
  int owner;
} gsl_vector;

Solution

  • According to the documentation:

    For consistency all memory is allocated through a gsl_block structure.

    Next:

    Vectors and matrices are made by slicing an underlying block.

    Basically, you can use an existing block of memory to get a new vector using, for example, (for some reason undocumented) functions alloc_from_block or alloc_from_vector. In such cases owner is set to 0 and when you free a vector that initial block stays allocated:

    void
    FUNCTION (gsl_vector, free) (TYPE (gsl_vector) * v)
    {
      RETURN_IF_NULL (v);
    
      if (v->owner)
        {
          FUNCTION(gsl_block, free) (v->block) ;
        }
      free (v);
    }