Valgrind says "stack allocation," I say "heap allocation"
I am trying to trace a segfault with valgrind. I get the following message from valgrind:
==3683== Conditional jump or move de开发者_如何学运维pends 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)
However, here's the offending line:
/* 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__);
}
What gives? I thought any call to malloc or calloc allocated heap space; there is no other variable allocated here, right? Is it possible there's another allocation going on (the offending stack allocation) that I'm not seeing?
EDIT: My current suspicion is a stack-allocated array: I declare a pointer to double (stack), then assign to it the result of a function that returns double *. Then I memmove it to a previously allocated place.
I can't memmove, memcpy or assign a stack variable then hope it will persist, can I?
I don't know what the problem is, but
-track-origins=yes
might help get you more information about what it's complaining about; see this blog post for details: http://blog.mozilla.com/nnethercote/2009/02/27/eliminating-undefined-values-with-valgrind-the-easy-way/
possible reason:
you define age_dep_data->mtable
as double*
but it should be double**
to be an array of arrays
I have since found that this valgrind error
Conditional jump or move depends on uninitialised value(s)
happens all the time and is not the source of the error. It appears to be a red herring in most cases I've encountered since posting this question.
精彩评论