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");
}

因为你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");
}

第一个head,它分配在堆栈上,而不是malloc分配的,所以不能释放,你要跳过它。

应该使用malloc(sizeof(StuList))分配链表结点内存,而不是使用malloc(sizeof(Student))来分配