M
Michel Rouzic
Hi,
I've recently met issues with my program which can only be explained
by heap corruption, so I've tried debugging my program with Valgrind,
and here's what I get with the following multidimensional array
allocation code :
typedef struct
{
int32_t speed;
int16_t height;
} lut_entry;
lut_entry ***lut; // <-global pointer
void lut_alloc()
{
...
lut = malloc(LUT_HC * sizeof(lut_entry **));
for (ih=0; ih<LUT_HC; ih++)
{
lut[ih] = malloc(LUT_DC * sizeof(lut_entry *));
for (id=0; id<LUT_DC; id++)
lut[ih][id] = calloc(LUT_AC, sizeof(lut_entry)); // <- line 1407
}
...
}
int main()
{
...
lut_alloc(); // <- line 1580
...
}
*********Valgrind output*********
==6051== Address 0x4FE8D5C is 4 bytes after a block of size 1,152
alloc'd
==6051== at 0x401F95F: calloc (vg_replace_malloc.c:279)
==6051== by 0x804C587: lut_alloc (main.c:1407)
==6051== by 0x804CECB: main (main.c:1580)
I've been allocating multidimensional arrays this way for years
(although not with structs until now), and I've never had such issues
(although I never used Valgrind before to verify that). Would anyone
know what's wrong with my code?
Thanks in advance.
I've recently met issues with my program which can only be explained
by heap corruption, so I've tried debugging my program with Valgrind,
and here's what I get with the following multidimensional array
allocation code :
typedef struct
{
int32_t speed;
int16_t height;
} lut_entry;
lut_entry ***lut; // <-global pointer
void lut_alloc()
{
...
lut = malloc(LUT_HC * sizeof(lut_entry **));
for (ih=0; ih<LUT_HC; ih++)
{
lut[ih] = malloc(LUT_DC * sizeof(lut_entry *));
for (id=0; id<LUT_DC; id++)
lut[ih][id] = calloc(LUT_AC, sizeof(lut_entry)); // <- line 1407
}
...
}
int main()
{
...
lut_alloc(); // <- line 1580
...
}
*********Valgrind output*********
==6051== Address 0x4FE8D5C is 4 bytes after a block of size 1,152
alloc'd
==6051== at 0x401F95F: calloc (vg_replace_malloc.c:279)
==6051== by 0x804C587: lut_alloc (main.c:1407)
==6051== by 0x804CECB: main (main.c:1580)
I've been allocating multidimensional arrays this way for years
(although not with structs until now), and I've never had such issues
(although I never used Valgrind before to verify that). Would anyone
know what's wrong with my code?
Thanks in advance.