我试图用valgrind跟踪段错误。我从valgrind那里收到以下消息:
==3683== Conditional jump or move depends on uninitialised value(s)
==3683== at 0x4C277C5: sparse_mat_mat_kron (sparse.c:165)
==3683== by 0x4C2706E: rec_mating (rec.c:176)
==3683== by 0x401C1C: age_dep_iterate (age_dep.c:287)
==3683== by 0x4014CB: main (age_dep.c:92)
==3683== Uninitialised value was created by a stack allocation
==3683== at 0x401848: age_dep_init_params (age_dep.c:131)
==3683==
==3683== Conditional jump or move depends on uninitialised value(s)
==3683== at 0x4C277C7: sparse_mat_mat_kron (sparse.c:165)
==3683== by 0x4C2706E: rec_mating (rec.c:176)
==3683== by 0x401C1C: age_dep_iterate (age_dep.c:287)
==3683== by 0x4014CB: main (age_dep.c:92)
==3683== Uninitialised value was created by a stack allocation
==3683== at 0x401848: age_dep_init_params (age_dep.c:131)
但是,这是违规行:
/* allocate mating table */
age_dep_data->mtable = malloc (age_dep_data->geno * sizeof (double *));
if (age_dep_data->mtable == NULL)
error (ENOMEM, ENOMEM, nullmsg, __LINE__);
for (int j = 0; j < age_dep_data->geno; j++)
{
131=> age_dep_data->mtable[j] = calloc (age_dep_data->geno, sizeof (double));
if (age_dep_data->mtable[j] == NULL)
error (ENOMEM, ENOMEM, nullmsg, __LINE__);
}
是什么给出的?我以为任何对malloc或calloc的调用都分配了堆空间;这里没有分配其他变量,对吧?是否有可能还有其他分配(违规堆栈分配),我没有看到?
编辑:我当前的怀疑是一个堆栈分配的数组:我声明一个指向double(堆栈)的指针,然后为它分配一个返回double *的函数的结果。然后我把它记忆到以前分配的地方。
我无法记住,memcpy或分配一个堆栈变量然后希望它会持续存在,是吗?
答案 0 :(得分:2)
我不知道问题是什么,但是
-track-origins=yes
可能会帮助您获得有关其抱怨内容的更多信息;有关详细信息,请参阅此博文 http://blog.mozilla.com/nnethercote/2009/02/27/eliminating-undefined-values-with-valgrind-the-easy-way/
答案 1 :(得分:0)
age_dep_data->mtable
定义为double*
但它应该是double**
是一个数组数组
答案 2 :(得分:-1)
我发现这个valgrind错误
Conditional jump or move depends on uninitialised value(s)
始终发生,并不是错误的来源。在发布这个问题之后的大多数情况下,它似乎是一个红色的鲱鱼。