Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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?

like image 862
Joel J. Adamson Avatar asked Apr 21 '10 14:04

Joel J. Adamson


People also ask

What is the difference between stack and heap allocation?

Key Differences Between Stack and Heap AllocationsIn a stack, the allocation and de-allocation are automatically done by the compiler whereas in heap, it needs to be done by the programmer manually. Handling of Heap frame is costlier than the handling of the stack frame.

Are allocations faster on stack or heap?

Because the data is added and removed in a last-in-first-out manner, stack-based memory allocation is very simple and typically much faster than heap-based memory allocation (also known as dynamic memory allocation) e.g. C's malloc .

What do you mean by heap allocation?

Heap allocation is the most flexible allocation scheme. Allocation and deallocation of memory can be done at any time and any place depending upon the user's requirement. Heap allocation is used to allocate memory to the variables dynamically and when the variables are no more used then claim it back.

Is heap allocation slower than stack?

Dynamic memory allocation and deallocation are very slow operations when compared to automatic memory allocation and deallocation. In other words, the heap is much slower than the stack.


1 Answers

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/

like image 158
Vicky Avatar answered Sep 23 '22 03:09

Vicky