马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
问题描述:执行下面程序时,到达释放内存的时候报错 中执行断点指令(__debugbreak()语句或类似调用)。
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define MAX_SIZE 100
struct Date {
int year;
int month;
int day;
};
struct Book {
char title[128];
char author[40];
float price;
struct Date date;
char publisher[40];
};
void getinput(struct Book* book);
void printbook(struct Book* book);
void initlibrary(struct Book* library[]);
void printlibrar(struct Book* library[]);
void releaselibrary(struct Book* library[]);
//初始化
void initlibrary(struct Book* library[])
{
for (int i = 0; i < MAX_SIZE; i++)
{
library[i] = NULL;
}
}
//录入信息
void getinput(struct Book* book)
{
printf("请输入书名:");
scanf_s("%s", book->title, 100);
printf("请输入作者:");
scanf_s("%s", book->author, 100);
printf("请输入售价:");
scanf_s("%f", &book->price);
printf("请输入出版日期:");
scanf_s("%d-%d-%d", &book->date.year, &book->date.month, &book->date.day);
printf("请输入出版社:");
scanf_s("%s", book->publisher, 100);
}
//打印信息
void printlibrar(struct Book* library[])
{
for (int i = 0; i < MAX_SIZE; i++)
{
if (library[i] != 0)
{
printbook(library[i]);
putchar('\n');
}
}
}
void printbook(struct Book* book)
{
printf("书名:%s\n", book->title);
printf("作者:%s\n", book->author);
printf("售价:%.2f\n", book->price);
printf("出版日期:%d-%d-%d\n", book->date.year, book->date.month, book->date.day);
printf("出版社:%s\n", book->publisher);
}
//释放内存空间
void releaselibrary(struct Book* library[])
{
int i = 0;
for (i = 0; i < MAX_SIZE; i++)
{
if (library[i] != NULL)
{
free(library[i]);
}
}
}
int main()
{
struct Book* library[MAX_SIZE];
struct Book* ptr = NULL;
int ch, index = 0;
initlibrary(library); //初始化
while (1)
{
printf("请问是否需要录入图书馆信息(Y / N)?:");
do {
ch = getchar();
} while (ch != 'Y' && ch != 'N');
if (ch == 'Y')
{
if (index < MAX_SIZE)
{
ptr = (struct Book*)malloc(sizeof(struct Book));
getinput(ptr);
library[index] = ptr;
index++;
putchar('\n');
}
else
{
printf("该图书馆已满,无法录入新数据!\n");
break;
}
}
else
{
break;
}
}
printf("\n 录入完毕,现在开始打印验证....\n\n");
printlibrar(library);
releaselibrary(library);
return 0;
}
|