鱼C论坛

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

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

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

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

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

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

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


  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <malloc.h>

  4. #define MAX_SIZE 100

  5. struct Date {
  6.         int year;
  7.         int month;
  8.         int day;
  9. };
  10. struct Book {
  11.         char title[128];
  12.         char author[40];
  13.         float price;
  14.         struct Date date;
  15.         char publisher[40];
  16. };

  17. void getinput(struct Book* book);
  18. void printbook(struct Book* book);
  19. void initlibrary(struct Book* library[]);
  20. void printlibrar(struct Book* library[]);
  21. void releaselibrary(struct Book* library[]);

  22. //初始化
  23. void initlibrary(struct Book* library[])
  24. {
  25.         for (int i = 0; i < MAX_SIZE; i++)
  26.         {
  27.                 library[i] = NULL;
  28.         }
  29. }

  30. //录入信息
  31. void getinput(struct Book* book)
  32. {
  33.         printf("请输入书名:");
  34.         scanf_s("%s", book->title, 100);
  35.         printf("请输入作者:");
  36.         scanf_s("%s", book->author, 100);
  37.         printf("请输入售价:");
  38.         scanf_s("%f", &book->price);
  39.         printf("请输入出版日期:");
  40.         scanf_s("%d-%d-%d", &book->date.year, &book->date.month, &book->date.day);
  41.         printf("请输入出版社:");
  42.         scanf_s("%s", book->publisher, 100);
  43. }

  44. //打印信息
  45. void printlibrar(struct Book* library[])
  46. {

  47.         for (int i = 0; i < MAX_SIZE; i++)
  48.         {
  49.                 if (library[i] != 0)
  50.                 {
  51.                         printbook(library[i]);
  52.                         putchar('\n');
  53.                 }
  54.         }
  55. }
  56. void printbook(struct Book* book)
  57. {
  58.         printf("书名:%s\n", book->title);
  59.         printf("作者:%s\n", book->author);
  60.         printf("售价:%.2f\n", book->price);
  61.         printf("出版日期:%d-%d-%d\n", book->date.year, book->date.month, book->date.day);
  62.         printf("出版社:%s\n", book->publisher);
  63. }

  64. //释放内存空间
  65. void releaselibrary(struct Book* library[])
  66. {
  67.         int i = 0;

  68.         for (i = 0; i < MAX_SIZE; i++)
  69.         {
  70.                 if (library[i] != NULL)
  71.                 {
  72.                         free(library[i]);
  73.                 }
  74.         }
  75. }

  76. int main()
  77. {
  78.         struct Book* library[MAX_SIZE];
  79.         struct Book* ptr = NULL;
  80.         int ch, index = 0;

  81.         initlibrary(library); //初始化

  82.         while (1)
  83.         {
  84.                 printf("请问是否需要录入图书馆信息(Y / N)?:");
  85.                 do {
  86.                         ch = getchar();
  87.                 } while (ch != 'Y' && ch != 'N');

  88.                 if (ch == 'Y')
  89.                 {
  90.                         if (index < MAX_SIZE)
  91.                         {
  92.                                 ptr = (struct Book*)malloc(sizeof(struct Book));
  93.                                 getinput(ptr);
  94.                                 library[index] = ptr;
  95.                                 index++;
  96.                                 putchar('\n');
  97.                         }
  98.                         else
  99.                         {
  100.                                 printf("该图书馆已满,无法录入新数据!\n");
  101.                                 break;
  102.                         }
  103.                 }
  104.                 else
  105.                 {
  106.                         break;
  107.                 }
  108.         }

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

  113.         return 0;
  114. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2024-6-6 22:12:14 | 显示全部楼层
  1. sh-5.2$ cat main.c
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <malloc.h>

  5. #define MAX_SIZE 100

  6. struct Date {
  7.         int year;
  8.         int month;
  9.         int day;
  10. };

  11. struct Book {
  12.         char title[128];
  13.         char author[40];
  14.         float price;
  15.         struct Date date;
  16.         char publisher[40];
  17. };

  18. // 初始化
  19. void initlibrary(struct Book *library[])
  20. {
  21.         for(int i = 0; i < MAX_SIZE; ++i)
  22.         {
  23.                 library[i] = NULL;
  24.         }
  25. }

  26. // 录入信息
  27. void getinput(struct Book *book)
  28. {
  29.         printf("请输入书名:");
  30.         scanf("%s", book->title);
  31.         printf("请输入作者:");
  32.         scanf("%s", book->author);
  33.         printf("请输入售价:");
  34.         scanf("%f", &book->price);
  35.         printf("请输入出版日期:");
  36.         scanf("%d-%d-%d", &book->date.year, &book->date.month, &book->date.day);
  37.         printf("请输入出版社:");
  38.         scanf("%s", book->publisher);
  39. }

  40. void printbook(struct Book *book)
  41. {
  42.         printf("书名:%s\n", book->title);
  43.         printf("作者:%s\n", book->author);
  44.         printf("售价:%.2f\n", book->price);
  45.         printf("出版日期:%d-%d-%d\n", book->date.year, book->date.month, book->date.day);
  46.         printf("出版社:%s\n", book->publisher);
  47. }

  48. // 打印信息
  49. void printlibrar(struct Book *library[])
  50. {

  51.         for(int i = 0; i < MAX_SIZE; ++i)
  52.         {
  53.                 if (library[i] != 0)
  54.                 {
  55.                         printbook(library[i]);
  56.                         putchar('\n');
  57.                 }
  58.         }
  59. }

  60. //释放内存空间
  61. void releaselibrary(struct Book *library[])
  62. {
  63.         for(int i = 0; i < MAX_SIZE; ++i)
  64.         {
  65.                 if (library[i] != NULL)
  66.                 {
  67.                         free(library[i]);
  68.                 }
  69.         }
  70. }

  71. int main(void)
  72. {
  73.         struct Book *library[MAX_SIZE];
  74.         struct Book *ptr = NULL;
  75.         int ch, index = 0;

  76.         initlibrary(library);   // 初始化

  77.         while(1)
  78.         {
  79.                 printf("请问是否需要录入图书馆信息(Y / N)?:");
  80.                 do {
  81.                         ch = getchar();
  82.                 } while (ch != 'Y' && ch != 'N');

  83.                 if(ch != 'Y') break;

  84.                 if(index < MAX_SIZE)
  85.                 {
  86.                         ptr = malloc(sizeof(struct Book));
  87.                         getinput(ptr);
  88.                         library[index++] = ptr;
  89.                         putchar('\n');
  90.                 }
  91.                 else
  92.                 {
  93.                         printf("该图书馆已满,无法录入新数据!\n");
  94.                         break;
  95.                 }
  96.         }

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

  100.         return 0;
  101. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-5-10 11:45

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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