I have a user-defined struct call MyStruct and allocate a 2D dynamic array:
MyStruct** arr = (MyStruct**) malloc(sizeof(myStruct*)*size);
I want to process the array in a function:
void compute(MyStruct** lst)
{
int index = 0;
while(lst[index] != NULL)
{
//do something
index++;
}
}
I called compute(arr)
and it works fine. But valgrind complains that there is an invalid read of size sizeof(MyStruct)
at line while(...)
. I understand that at this point index is out of bound by 1 element. An easy fix is to pass size
to the function and check if index < size
through the loop.
Out of curiosity, is there anyway I can still traverse through the array without indexing that extra element AND not passing size
to the function?