realloc invalid pointer C

65 views Asked by At

I have realloc () : invalid pointer when try to initUniversity Test We have a binary file which consist of students. I need to add all of them in University, but i have this error. What's the problem? Here is function to add new group

bool addNewGroup(University *university, const Group group) {
    if (university == NULL) {
        return false;
    }
    university->groupsCount += 1;
    Group *t;
    if (university->groupsCount == 1) {
        if (!(university->groups = calloc(1, sizeof(Group)))) {
            return false;
        }
        university->groups[0] = group;
        return true;
    }
    if (!(t = realloc(university->groups, 
        university->groupsCount  * sizeof(Group)))) {
        return false;
    }
    university->groups = t;
    university->groups[university->groupsCount - 1] = group;
    strcpy(university->groups[university->groupsCount - 1].name, group.name);
    return true;
}

char *_strtoupper(char *str) {
    if (str == NULL) {
        return NULL;
    }
    for (int i = 0; i < strlen(str); ++i) {
        if (str[i] > 96 && str[i] < 123) {
                str[i] -=32;
        }
    }
    return str;

}

int strcm(char *s, char *st) {
    char cs[SIZE];
    char cst[SIZE];
    strcpy(cs, s);
    strcpy(cst, st);
    return strcmp(_strtoupper(cs), _strtoupper(cst));
}
here is add new student

bool addNewStudent(Group *group, Student student) {
    if (group == NULL) {
        return false;
    }
    int ind = 0;
    int fl = 0;
    if (student.id == 0) {
        student.id = g_Id;
        ++g_Id;
    }
    ++group->studentsCount;
    Student *t;
    if (group->studentsCount == 1) {
        if (!(group->students = calloc(1, sizeof(Student)))) {
            return false;
        }
        group->students[0] = student;
        return true;     
    }
    if (!(t = realloc(group->students, group->studentsCount * sizeof(Student)))) {
        return false;
    }
    group->students = t;
    group->students[group->studentsCount - 1] = student;
    Student tmp;
    for (int i = 0; i < group->studentsCount - 1; ++i) {
        for (int j = 0; j < group->studentsCount - 1 - i; ++j) { 
            if (strcm(group->students[j].surname, group->students[j + 1].surname) > 0) {
                tmp = group->students[j];
                group->students[j] = group->students[j + 1];
                group->students[j + 1] = tmp; 
            }
        }
    } 
    return true;
}

here is problem function 

University* initUniversity(const char *fileName) {
    University* un;
    if (!(un = (University *)calloc(1, sizeof(University)))) {
        return un;
    }
    if (fileName == NULL) {
        return un;
    }
    FILE *fd = fopen(fileName, "rb");
    if (fd == NULL) {
        return un;
    }
    Student student;
    Group group;
    char gr[SIZE] = "";
    int i = -1;
    int j = 0;
    unsigned max = 0;
    while (fread(&student,sizeof(Student), 1, fd)) {
        if (student.id > max ) {
            g_Id = student.id + 1;
            max = student.id;
        }
        if (strcmp(gr, student.groupName) != 0) {
            ++i;
            j = 0;
            strcpy(group.name, student.groupName);
            addNewGroup(un, group);
        }
        addNewStudent(&un->groups[i], student);
        strcpy(gr, student.groupName);
        }
    fclose(fd); 
    return un;
}

here is stuctures used in work

typedef struct
{
    unsigned long id;       // Уникальный id, получаемый из g_Id
    char name[SIZE];        // Имя
    char surname[SIZE];     // Фамилия
    char groupName[SIZE];   // Название группы
    unsigned birthYear;     // Год рождения
} Student;

// Структура описывающая группу 
// Cоздается при чтении, если getGroup() вернула NULL; название группы берется из структуры студента
typedef struct
{
    char name[SIZE];        // Название группы
    unsigned studentsCount; // Количество студентов в группе (размер динамического массива)
    Student *students;      // Динамический массив со студентами
} Group;

// Структура описывающая университет
typedef struct
{
    unsigned groupsCount;   // Количество групп в университете
    Group *groups;          // Динамический массив с группами
} University;

I really don't understand what is wrong

0

There are 0 answers