views:

379

answers:

3

Dear Friends,

I am trying to trace a segfault with valgrind. I get the following message from 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)

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?

A: 

possible reason:
you define age_dep_data->mtable as double* but it should be double** to be an array of arrays

oraz
Is it what I'm doing? I allocate an array of size n*(size of a pointer to double), then for each pointer in that array I allocate an array of size m*(size of a double). Do I need to use sizeof(double **) in the call to malloc()?
Joel J. Adamson
no, you code is okey; we can't see the declaration of age_dep_data->mtable - it should be declared as double**
oraz
It is declared as double ** within a structure (also dynamically allocated).
Joel J. Adamson
ok, then i pass
oraz
+1  A: 

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/

Vicky
I'm using track-origins; I forgot to mention that. Could it be leading me astray?
Joel J. Adamson
A: 

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.

Joel J. Adamson