I am passing a struct through a function like so...
expandArrayofStructs(Container *container, int n)
This container is one struct and inside of that struct is an array of another type of struct I'll call it inner.
I would like to expand the size of the array of inner by some value and then continue in my original function after the array has been expanded. So this expand function does not return anything rather it is just called and expands the data and finishes and the program continues with a new larger array than before.
My understanding of the situation would be something like this but this does not work properly...
int expandArrayofStructs(Container *container, int n)
{
container->inner = realloc(container->inner, sizeof(inner) * 50);
^
Just a number i picked. so if i
already had an array of 50
I would be increasing by 50 here.
if(Container->inner == NULL)
//HANDLE ERROR IF REALLOC FAILS
//Update the container length
container->length = container->length + 50;
//For some reason the specs of the program say I need to return
//the array length which is an attribute of container
return container->length;
}
But when I realloc in this manner I am not even getting segfault error I am getting: realloc(): invalid next size 0x463829
the numbers at the end vary.
Not sure what I am doing wrong, but if there is a better way to dynamically realloc an array of structs then I am open to suggestion. This particular code does not have to be exactly what it is.
The only stipulation is that this function returns type int which is = the new array length