鱼C论坛

 找回密码
 立即注册
查看: 137|回复: 1

microsoft visual c++ 报错:中执行断点指令(__debugbreak()语句或类似调用)。

[复制链接]
发表于 2024-6-6 15:30:36 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
问题描述:执行下面程序时,到达释放内存的时候报错 中执行断点指令(__debugbreak()语句或类似调用)。

中执行断点指令(__debugbreak()语句或类似调用).png

#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;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2024-6-6 22:12:14 | 显示全部楼层
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[128];
        char author[40];
        float price;
        struct Date date;
        char publisher[40];
};

// 初始化
void initlibrary(struct Book *library[])
{
        for(int i = 0; i < MAX_SIZE; ++i)
        {
                library[i] = 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[i] != 0)
                {
                        printbook(library[i]);
                        putchar('\n');
                }
        }
}

//释放内存空间
void releaselibrary(struct Book *library[])
{
        for(int i = 0; i < MAX_SIZE; ++i)
        {
                if (library[i] != NULL)
                {
                        free(library[i]);
                }
        }
}

int main(void)
{
        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') break;

                if(index < MAX_SIZE)
                {
                        ptr = malloc(sizeof(struct Book));
                        getinput(ptr);
                        library[index++] = ptr;
                        putchar('\n');
                }
                else
                {
                        printf("该图书馆已满,无法录入新数据!\n");
                        break;
                }
        }

        printf("\n 录入完毕,现在开始打印验证....\n\n");
        printlibrar(library);
        releaselibrary(library);

        return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-6-26 11:46

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表