Valgrind reports error Invalid read of size 8
in the following code.
I have an array declared like,
struct symbol *st[PARSER_HASH_SIZE];
When my program is initialized, all the elements in this array are initailzied as 0.
memset(&st[0], 0, sizeof(st));
My program creates instances of struct symbol
and inserts into the above array depending on the hash value. So few of the elements in this array will be NULL and others will be valid value.
The following code tries to delete the allocated items and valgrind complains at the line, sym = st[i]; sym != NULL; sym = sym->next
struct symbol *sym = NULL; /* cleaning the symbol table entries */ for(i = 0; i < PARSER_HASH_SIZE; i++) { for(sym = st[i]; sym != NULL; sym = sym->next) { /* <-- Valgrind complains here */ free(sym); } }
I am trying to understand the reason for this error.
Any help would be great!
An Invalid read means that the memory location that the process was trying to read is outside of the memory addresses that are available to the process. size 8 means that the process was trying to read 8 bytes. On 64-bit platforms this could be a pointer, but also for example a long int.
Invalid reads and writes can be fixed by making sure to set any unused pointer to NULL and being careful about the memory locations that you access in your code. int x; printf("x = %d\n\r", x);
1 Answer. Show activity on this post. It looks like Valgrind is complaining because you have only allocated 1780 bytes for the PetscObject, and this 8-byte write (starting at byte 1776) is enough to write into memory you didn't allocate.
Invalid read It means that we tried to read 4 bytes, starting at adress 0x0 (for those of you who don't know it yet, NULL is actually a pointer to adress 0x0, so we tried to read 4 bytes starting from NULL).
The problem is that you're freeing the sym
, then trying to access a value from the (now-freed) data: sym->next
.
You probably want something like this for the inner loop:
struct symbol *next_sym = NULL; for(sym = st[i]; sym != NULL; ) { next_sym = sym->next; free(sym); sym = next_sym; }
also its not clear if you array is meant to contain structs or pointers to structs
struct symbol *st[PARSER_HASH_SIZE];
says its an array of pointers to structs. But then you say
"When my program is initialized, all the elements in this array are initailzied as 0."
memset(&st[0], 0, sizeof(st));
This is treating the entries like structs
to clear the array do
for (int i = 0; i < PARSER_HASH_SIZE; i++) { st[i] = 0; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With