gsl_vector 如何拥有 gsl_block?

How does a gsl_vector own a gsl_block?

在GSL的参考手册中写道

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.

但是'If the vector owns the block'是什么意思?

下面是gsl_vector的结构

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

根据文档:

For consistency all memory is allocated through a gsl_block structure.

下一个:

Vectors and matrices are made by slicing an underlying block.

基本上,您可以使用现有的内存块来获取新向量,例如,(由于某种原因未记录)函数 alloc_from_blockalloc_from_vector。在这种情况下,owner 设置为 0,当您释放一个初始块保持分配的向量时:

void
FUNCTION (gsl_vector, free) (TYPE (gsl_vector) * v)
{
  RETURN_IF_NULL (v);

  if (v->owner)
    {
      FUNCTION(gsl_block, free) (v->block) ;
    }
  free (v);
}