MST

星途 面试题库

面试题:C语言结构体嵌套与动态内存管理的优化

定义一个结构体表示班级,班级结构体中包含一个教师结构体指针(教师结构体包含姓名和教龄),以及一个学生结构体数组指针(学生结构体包含姓名、年龄和成绩)。请编写函数实现班级的创建,要求合理分配内存,并且在函数结束后能正确释放所有分配的内存,同时要考虑到输入数据可能存在的错误处理。
25.9万 热度难度
编程语言C

知识考点

AI 面试

面试题答案

一键面试
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// 定义教师结构体
typedef struct Teacher {
    char name[50];
    int yearsOfTeaching;
} Teacher;

// 定义学生结构体
typedef struct Student {
    char name[50];
    int age;
    float score;
} Student;

// 定义班级结构体
typedef struct Class {
    Teacher *teacher;
    Student *students;
    int studentCount;
} Class;

// 创建班级的函数
Class* createClass(const char* teacherName, int teacherYears, const char** studentNames, int* studentAges, float* studentScores, int studentCount) {
    if (teacherName == NULL || studentNames == NULL || studentAges == NULL || studentScores == NULL || studentCount <= 0) {
        return NULL;
    }

    Class* newClass = (Class*)malloc(sizeof(Class));
    if (newClass == NULL) {
        return NULL;
    }

    newClass->teacher = (Teacher*)malloc(sizeof(Teacher));
    if (newClass->teacher == NULL) {
        free(newClass);
        return NULL;
    }
    strcpy(newClass->teacher->name, teacherName);
    newClass->teacher->yearsOfTeaching = teacherYears;

    newClass->students = (Student*)malloc(studentCount * sizeof(Student));
    if (newClass->students == NULL) {
        free(newClass->teacher);
        free(newClass);
        return NULL;
    }

    for (int i = 0; i < studentCount; i++) {
        strcpy(newClass->students[i].name, studentNames[i]);
        newClass->students[i].age = studentAges[i];
        newClass->students[i].score = studentScores[i];
    }

    newClass->studentCount = studentCount;
    return newClass;
}

// 释放班级内存的函数
void freeClass(Class* class) {
    if (class != NULL) {
        if (class->teacher != NULL) {
            free(class->teacher);
        }
        if (class->students != NULL) {
            free(class->students);
        }
        free(class);
    }
}

你可以使用以下方式调用这个函数:

int main() {
    const char* teacherName = "Mr. Smith";
    int teacherYears = 10;
    const char* studentNames[] = {"Alice", "Bob", "Charlie"};
    int studentAges[] = {18, 19, 20};
    float studentScores[] = {85.5, 90.0, 78.0};
    int studentCount = 3;

    Class* myClass = createClass(teacherName, teacherYears, studentNames, studentAges, studentScores, studentCount);
    if (myClass != NULL) {
        // 使用班级数据
        printf("Teacher: %s, Years of Teaching: %d\n", myClass->teacher->name, myClass->teacher->yearsOfTeaching);
        for (int i = 0; i < myClass->studentCount; i++) {
            printf("Student %d: %s, Age: %d, Score: %.2f\n", i + 1, myClass->students[i].name, myClass->students[i].age, myClass->students[i].score);
        }
        freeClass(myClass);
    } else {
        printf("Failed to create class.\n");
    }

    return 0;
}

代码说明:

  1. 结构体定义:分别定义了TeacherStudentClass结构体,Class结构体中包含Teacher指针和Student数组指针。
  2. createClass函数
    • 进行输入参数的有效性检查,如果输入参数有NULL或者学生数量小于等于0,返回NULL
    • Class结构体分配内存,如果分配失败返回NULL
    • Teacher结构体分配内存,如果分配失败,释放Class结构体并返回NULL
    • Student数组分配内存,如果分配失败,释放Teacher结构体和Class结构体并返回NULL
    • 填充教师和学生的数据。
    • 返回创建好的Class结构体指针。
  3. freeClass函数
    • 释放Class结构体中teacherstudents分配的内存,最后释放Class结构体本身。
  4. main函数
    • 调用createClass函数创建班级并检查是否创建成功。
    • 如果创建成功,输出班级信息。
    • 调用freeClass函数释放班级内存。