Let's suppose I have this function:
void arrayExtendDouble(int **ptArr, int *size)
{
*ptArr = realloc(*ptArr, (*size * 2) * sizeof(int));
for(int i = (*size * 2) - 1; i >= *size; i--)
ptArr[i] = fib(i); //this will throw SEG FAULT
*size *= 2;
}
Note: I am a student and this is the valid resolution a teacher gave.
Now, the only way i can make this work is like this:
void fibArrayExpand(int **ptArr, int *size)
{
int *ptArrNew = realloc(*ptArr, (*size * 2) * sizeof(int));
for(int i = (*size * 2) - 1; i >= *size; i--)
ptArrNew[i] = fib(i);
*size *= 2;
*ptArr = ptArrN;
}
Supposedly the first one (teacher's) is correct and the second one (mine) it's not because i do extra steps not needed.
I would like to know why does it throw segmentation fault, is it supposed to do so or is the function well written?
The first snippet isn't correct.
ptAtrisn't the pointer to the ints; it's a pointer to another pointer,*ptAtr, which is the pointer to the ints. As such,should be
Alternate explanation
It's pretty easy to see the following code achieves the correct result:
You might notice that
arrand*arr_ptrhave the same value, and so dosizeandsize_ptr. That means we could simply replace all instances ofarrandsizewith*arr_ptrand*size_ptrrespectively.Note that
(*arr_ptr)[i] = fib(i);is used instead ofarr[i] = fib(i);. The first snippet you posted is therefore incorrect.