David0104 2016-05-28 10:39 采纳率: 100%
浏览 1299
已采纳

free释放链表内存总是出错,求高手指点!!

#include
#include
#include
using namespace std;
#define TRUE 1
#define FALSE 0
typedef struct Student
{
char num[10];
char name[10];
char grade[10];
}Student;

typedef struct StuList
{
Student data;
StuList *next;
}StuList, *lpStuList;

lpStuList stuList = NULL;//全局的学生信息链表
lpStuList head = NULL; //全局的链表头节点

void menu()
{
cout << "选择(1:输入数据 2:输出数据 3:按姓名查找数据 其他:退出):" << endl;
}

int addStu()
{
int num;
cout << "输入数据" << endl << "学生人数:";
cin >> num;
if (stuList == NULL)
{
stuList = (lpStuList)malloc(sizeof(Student));
if (stuList != NULL)
{
stuList->next = NULL;
}
}
lpStuList cur = NULL;
for (int i = 1; i <= num; ++i)
{
Student student;
cur = NULL;
cur = (lpStuList)malloc(sizeof(Student));
cout << "第" << i << "个学生(学号 姓名 成绩):";
cin >> student.num >> student.name >> student.grade;
cur->data = student;
cur->next = NULL;
if (head == NULL)
{
head = cur;
}
else stuList->next = cur;
stuList = cur;
}
return TRUE;
}

int display()
{
cout << "输出数据" << endl;
lpStuList cur = NULL;
cur = head;
if (cur == NULL)
{
cout << "没有任何学生信息哦" << endl;
return FALSE;
}
else
{
cout << "学号\t\t姓名\t\t成绩" << endl;
while (cur != NULL)
{
cout << cur->data.num << "\t\t" << cur->data.name << "\t\t" << cur->data.grade << endl;
cur = cur->next;
}
return TRUE;
}
}

void search()
{
char name[10];
cout << "按姓名查找数据" << endl << "请输入姓名:";
cin >> name;
lpStuList cur = NULL;
cur = head;
int ok = 0;
while (cur != NULL)
{
if (strstr(cur->data.name, name) != NULL)
{
if (ok == 0)
{
cout << "学号\t\t姓名\t\t成绩" << endl;
ok = 1;
}
cout << cur->data.num << "\t\t" << cur->data.name << "\t\t" << cur->data.grade << endl;
}
cur = cur->next;
}
if (ok == 0)
{
cout << "未找到符合的信息哦" << endl;
}
}

void exitApp()
{
if (head != NULL)
{
lpStuList cur = NULL;
while (head != NULL)
{
cur = head;
head = head->next;
free(cur); // 运行到这里总会崩溃
cur = NULL;
}
}
}

int main()
{
int start = 1;
while (start)
{
menu();
int num;
cin >> num;
switch (num)
{
case 1: {addStu(); break; }
case 2: {display(); break; }
case 3: {search(); break; }
default: {exitApp(); start = 0; }
}
}
system("pause");
}

  • 写回答

3条回答 默认 最新

  • 小灸舞 2016-05-28 13:15
    关注

    因为你malloc的空间太小,导致越界写入。破坏了堆空间,所以free出错
    (lpStuList)malloc(sizeof(Student));应该改成(lpStuList)malloc(sizeof(StuList));

     #include <iostream>
    #include <iomanip>
    #include <string>
    using namespace std;
    #define TRUE 1
    #define FALSE 0
    typedef struct Student
    {
        char num[10];
        char name[10];
        char grade[10];
    }Student;
    
    typedef struct StuList
    {
        Student data;
        StuList *next;
    }StuList, *lpStuList;
    
    lpStuList stuList = NULL;//全局的学生信息链表
    lpStuList head = NULL; //全局的链表头节点
    
    void menu()
    {
        cout << "选择(1:输入数据 2:输出数据 3:按姓名查找数据 其他:退出):" << endl;
    }
    
    int addStu()
    {
        int num;
        cout << "输入数据" << endl << "学生人数:";
        cin >> num;
        if (stuList == NULL)
        {
            stuList = (lpStuList)malloc(sizeof(StuList));
            if (stuList != NULL)
            {
                stuList->next = NULL;
            }
        }
        lpStuList cur = NULL;
        for (int i = 1; i <= num; ++i)
        {
            Student student;
            cur = NULL;
            cur = (lpStuList)malloc(sizeof(StuList));
            cout << "第" << i << "个学生(学号 姓名 成绩):";
            cin >> student.num >> student.name >> student.grade;
            cur->data = student;
            cur->next = NULL;
            if (head == NULL)
            {
                head = cur;
            }
            else stuList->next = cur;
            stuList = cur;
        }
        return TRUE;
    }
    
    int display()
    {
        cout << "输出数据" << endl;
        lpStuList cur = NULL;
        cur = head;
        if (cur == NULL)
        {
            cout << "没有任何学生信息哦" << endl;
            return FALSE;
        }
        else
        {
            cout << "学号\t\t姓名\t\t成绩" << endl;
            while (cur != NULL)
            {
                cout << cur->data.num << "\t\t" << cur->data.name << "\t\t" << cur->data.grade << endl;
                cur = cur->next;
            }
            return TRUE;
        }
    }
    
    void search()
    {
        char name[10];
        cout << "按姓名查找数据" << endl << "请输入姓名:";
        cin >> name;
        lpStuList cur = NULL;
        cur = head;
        int ok = 0;
        while (cur != NULL)
        {
            if (strstr(cur->data.name, name) != NULL)
            {
                if (ok == 0)
                {
                    cout << "学号\t\t姓名\t\t成绩" << endl;
                    ok = 1;
                }
                cout << cur->data.num << "\t\t" << cur->data.name << "\t\t" << cur->data.grade << endl;
            }
            cur = cur->next;
        }
        if (ok == 0)
        {
            cout << "未找到符合的信息哦" << endl;
        }
    }
    
    void exitApp()
    {
        if (head != NULL)
        {
            lpStuList cur = NULL;
            while (head != NULL)
            {
                cur = head;
                head = head->next;
                free(cur); // 运行到这里总会崩溃
                cur = NULL;
            }
        }
    }
    
    int main()
    {
        int start = 1;
        while (start)
        {
            menu();
            int num;
            cin >> num;
            switch (num)
            {
            case 1: {addStu(); break; }
            case 2: {display(); break; }
            case 3: {search(); break; }
            default: {exitApp(); start = 0; }
            }
        }
        system("pause");
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?