First of all I want to apologize if this is a repeated question. But I could not find any answer explaining how to do this correctly with a variable sized array.
Going straight to the code, I've got this:
void f(int[][2]);
int main(void)
{
int (*obsF)[2] = malloc(sizeof(int)); //I reserve some memory so I can pass
f(obsF); //the array to f, since I don't know
free(obsF); //its size until later working with f.
}
void f(int obsF[][2])
{
obsF = realloc(obsF, sizeof(int[5][2])); //The 5 is just as an example.
// do stuff with obsF //I won't know this number until
} //later in f.
Testing this with valgrind threw that that free(obsF);
is an invalid free().
Now, if I do the same but everything in the main function like this:
int main(void)
{
int (*obsF)[2] = malloc(sizeof(int));
obsF = realloc(obsF, sizeof(int[5][2]));
free(obsF);
}
Valgrind tests passed successfully with no errors.
I would like to know why this is happening and how to realloc my 2D array inside function and be able to free it from main correctly.
Any other better approach or solution is well received, as long as I can use obsF as a 2D array inside f and modify it as I wish and then use it in main.
Thanks.-