microsoft visual c++ 报错:中执行断点指令(__debugbreak()语句或类似调用)。
问题描述:执行下面程序时,到达释放内存的时候报错 中执行断点指令(__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;
char author;
float price;
struct Date date;
char publisher;
};
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 = 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 != 0)
{
printbook(library);
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 != NULL)
{
free(library);
}
}
}
int main()
{
struct Book* library;
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 = ptr;
index++;
putchar('\n');
}
else
{
printf("该图书馆已满,无法录入新数据!\n");
break;
}
}
else
{
break;
}
}
printf("\n 录入完毕,现在开始打印验证....\n\n");
printlibrar(library);
releaselibrary(library);
return 0;
} sh-5.2$ cat main.c
#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;
char author;
float price;
struct Date date;
char publisher;
};
// 初始化
void initlibrary(struct Book *library[])
{
for(int i = 0; i < MAX_SIZE; ++i)
{
library = NULL;
}
}
// 录入信息
void getinput(struct Book *book)
{
printf("请输入书名:");
scanf("%s", book->title);
printf("请输入作者:");
scanf("%s", book->author);
printf("请输入售价:");
scanf("%f", &book->price);
printf("请输入出版日期:");
scanf("%d-%d-%d", &book->date.year, &book->date.month, &book->date.day);
printf("请输入出版社:");
scanf("%s", book->publisher);
}
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 printlibrar(struct Book *library[])
{
for(int i = 0; i < MAX_SIZE; ++i)
{
if (library != 0)
{
printbook(library);
putchar('\n');
}
}
}
//释放内存空间
void releaselibrary(struct Book *library[])
{
for(int i = 0; i < MAX_SIZE; ++i)
{
if (library != NULL)
{
free(library);
}
}
}
int main(void)
{
struct Book *library;
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') break;
if(index < MAX_SIZE)
{
ptr = malloc(sizeof(struct Book));
getinput(ptr);
library = ptr;
putchar('\n');
}
else
{
printf("该图书馆已满,无法录入新数据!\n");
break;
}
}
printf("\n 录入完毕,现在开始打印验证....\n\n");
printlibrar(library);
releaselibrary(library);
return 0;
}
页:
[1]