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?