Hello all, I am creating a program that uses a dynamic array to store students. My code compiles but i keep getting a runtime error which prints out a bunch of stuff, but the main thing being:
*** glibc detected *** ./a.out: realloc(): invalid pointer: 0xb7fc2e00 ***
so apparently there is a problem with my realloc but I just can't seem to figure out what it is. As far as I can see it should work. Any help with this would be greatly appreciated.
Here is my main which is pointless right now. Basically it's just set up to test whether the add function works.
And my header file
int number_of_elements = 0;
int array_size = 0;
std1.lastname = "smith";
std1.firstname = "bob";
std1.score = 50;
add(array, std1, &number_of_elements, &array_size);
and finally the important one. The student file which contains the dynamic memory function. Again the error seems to be on the realloc line.
typedef struct student
void add(student*, student, int*, int*);
void add(student *array, student item, int *number_of_elements, int *array_size)
if (*number_of_elements == *array_size) /*resize array*/
if (*array_size == 0)
*array_size = 1;
*array_size = *array_size * 2;
array = (student*)realloc(array, ((*array_size) * sizeof(student)));
if (array == 0)
printf("no space for memory");
array[*number_of_elements] = item;