Reputation: 141887
I am allocating enough memory for two pointers which I treat as arrays:
// Allocate memory for both adj and deg
int *adjdeg = malloc(sizeof(int)*n*n);
adj = adjdeg;
deg = adjdeg + n*n - n;
I am then using GNU's bzero
from string.h to initialize the values in the deg
"array" to 0 (the adj
"array" doesn't need to be initialized to it since I write to it before I ever read from it). This works fine, and my program runs successfully, but valgrind reports many errors about use of initialized values whenever I read from deg
(IE. deg[0]
). Here is my call to bzero
:
// My bzero call
bzero(deg, n);
valgrind is happy if I remove my call to bzero and use a loop like:
int i;
for(i = 0; i < n; i++)
deg[i] = 0;
Is there any way to tell valgrind that bzero is initializing that area in memory correctly? I'm using gcc 4.6.3 and valgrind 3.7.0
Note: I get the same errors in valgrind if I use memset
instead of bzero
.
Upvotes: 1
Views: 255
Reputation: 754590
You should be using:
bzero(deg, n * sizeof(int));
As it is, you're initializing n
bytes, not n
integers. For maximal portability, you should probably use memset()
instead of bzero()
, but you still need the sizeof(int)
multiplier in the size:
memset(deg, '\0', n * sizeof(int));
Upvotes: 6