简体   繁体   English

动态分配和GSL(Gnu科学图书馆)

[英]Dynamically Allocation and GSL(Gnu Scientific Library)

So, I have to use this function from GSL. 因此,我必须使用GSL的此功能。 This one: 这个:

gsl_matrix_view_array (double * base, size_t n1, size_t n2)

The first argument ( double * base ) is the matrix I need to pass to it, which is read as input from the user. 第一个参数( double * base )是我需要传递给它的矩阵,该矩阵作为用户的输入读取。

I'm dynamically allocating it this way: 我以这种方式动态分配它:

double **base;
base = malloc(size*sizeof(double*));

for(i=0;i<size;i++)
base[i] = malloc(size*sizeof(double));

Where size is given by the user. 尺寸由用户指定。 But then, when the code runs, it warns this : 但是,当代码运行时,它会发出警告:

  "passing arg 1 of gsl_matrix_view_array from incompatible pointer type".

What is happening? 怎么了?

The function expects a flat array, eg, double arr[size*size]; 该函数需要一个平面数组,例如double arr[size*size]; .

Here's an example from the documentation that I have slightly modified to use a matrix view: 这是文档中的一个示例,我对其进行了稍微修改以使用矩阵视图:

#include <stdio.h>
#include <stdlib.h>
#include <gsl/gsl_matrix.h>

int main(void) {
  int i, j; 

  double *arr = malloc(10 * 3 * sizeof*arr);
  gsl_matrix_view mv = gsl_matrix_view_array(arr, 10, 3);
  gsl_matrix * m = &(mv.matrix);

  for (i=0; i<10; i++)
    for (j=0; j<3; j++)
      gsl_matrix_set(m, i, j, 0.23 + 100*i + j);

  for (i=0; i<10; i++)
    for (j=0; j<3; j++)
      printf("m(%d,%d) = %g\n", i, j, gsl_matrix_get(m, i, j));

  free(arr);

  return 0;
}

Note that you can also directly allocate memory for the matrix using the provided API. 请注意,您还可以使用提供的API为矩阵直接分配内存。

Here's the original example: 这是原始示例:

#include <stdio.h>
#include <gsl/gsl_matrix.h>

int main(void)
{
  int i, j; 

  gsl_matrix * m = gsl_matrix_alloc(10, 3);

  for (i=0; i<10; i++)
    for (j=0; j<3; j++)
      gsl_matrix_set(m, i, j, 0.23 + 100*i + j);

  for (i=0; i<10; i++)
    for (j=0; j<3; j++)
      printf("m(%d,%d) = %g\n", i, j, gsl_matrix_get(m, i, j));

  gsl_matrix_free(m);

  return 0;
}

For reference: 以供参考:

gsl_matrix_view_array expects your matrix as a contiguous single allocation in row-major order. gsl_matrix_view_array期望矩阵以行优先的顺序作为连续的单个分配。 You should be allocating your array like this: 您应该像这样分配数组:

double (*ar)[size] = malloc(sizeof(ar[size]) * size);

Then (after populating it) 然后(填充后)

gsl_matrix_view_array(ar[0], size, size);

Finally, free your allocation when done with a single call: 最后,只需一次调用即可释放您的分配:

free(ar);

Note: Don't try this with C++, as VLA's aren't standard-supported for that language. 注意:请勿在C ++中尝试此操作,因为该语言不标准支持VLA。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 GNU科学图书馆(GSL)总结 - Sums on GNU Scientific Library (GSL) 调用gsl_blas_ddot时,gsl gnu科学库分割错误 - gsl gnu scientific library segmentation fault when calling gsl_blas_ddot 使用GNU Scientific Library(GSL)使用不均匀间隔的点绘制2D B样条曲线路径 - Using GNU Scientific Library (GSL) to draw a 2D B-Spline path using unevenly spaced points 使用GNU Scientific Library(GSL)的C / C ++代码给GNUPlot带来了不同的结果 - 可能的浮点不稳定性? - C/C++ code with GNU Scientific Library (GSL) gives different results to GNUPlot - possible floating point instabilities? 如何使用 emscripten 将 C GNU 科学库 (GSL) 编译为 web 程序集? - How to compile the C GNU Scientific Library (GSL) to web assembly using emscripten? GNU GSL BLAS库,未定义符号 - GNU GSL BLAS library, undefined symbols 使用C语言的GNU科学库进行线性拟合 - Linear fitting with GNU Scientific Library in C 让Gnu科学图书馆在Cygwin中工作 - Getting the Gnu Scientific Library to work in Cygwin 将GNU科学库与Code :: Blocks一起使用时出错 - Error using GNU scientific library with Code::Blocks GNU科学图书馆的概率分布函数 - GNU Scientific Library probability distribution functions in C
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM