条件跳转或移动取决于未初始化的值 - 使用gsl_matrix_set

时间:2016-11-16 18:10:06

标签: c runtime-error gsl

我正在尝试在C中使用GSL库并遇到麻烦。

我为我的矩阵和向量分配空间:

A   = gsl_matrix_alloc(nr, nc); /* Data matrix */
tau = gsl_vector_alloc(nc);     /* required place holder for GSL */

然后我正在初始化A

for(i = 0; i < nr; i++){
  gsl_matrix_set(A, i, 0, 1.0); 
}

for(i = 1; i < nr; i++){
  for(j = 0; j < nc; j++){
    gsl_matrix_set(A, i, j, 1.0 / (double)(i + j + 1)); 
  }
}

然后我在这一行上出错:

gsl_linalg_QR_decomp(A, tau);

出现以下错误:

==901== Conditional jump or move depends on uninitialised value(s)
==901==    at 0x3E8920AE0E: cblas_dnrm2 (in /usr/lib64/libgslcblas.so.0.0.0)
==901==    by 0x3E88EA779A: gsl_linalg_householder_transform (in /usr/lib64/libgsl.so.0.14.0)
==901==    by 0x3E88EA1305: gsl_linalg_QR_decomp (in /usr/lib64/libgsl.so.0.14.0)
==901==    by 0x400FF3: GE_FindPoint (hw8.c:145)
==901==    by 0x400DF4: main (hw8.c:66)
==901==  Uninitialised value was created by a heap allocation
==901==    at 0x4A06A2E: malloc (vg_replace_malloc.c:270)
==901==    by 0x3E88E4CFD4: gsl_block_alloc (in /usr/lib64/libgsl.so.0.14.0)
==901==    by 0x3E88EB1768: gsl_matrix_alloc (in /usr/lib64/libgsl.so.0.14.0)
==901==    by 0x400EBB: GE_FindPoint (hw8.c:110)
==901==    by 0x400DF4: main (hw8.c:66)

第110行指向分配A的行:A = gsl_matrix_alloc(nr, nc);

在这种情况下,nc = 3nr = 3840。什么会导致这个错误?

1 个答案:

答案 0 :(得分:0)

在初始化的第二部分中,ij的起始值不匹配。所以你拨打gsl_matrix_set( A, i, 0, ... )两次,因为1&lt; = i&lt;你永远不会因为{&lt; = j&lt; NC

只需将初始化的那一部分改为

即可
gsl_matrix_set( A, 0, j, ... )