鱼C论坛

 找回密码
 立即注册
查看: 1662|回复: 5

[已解决]请问42行这里为什么会出现段错误

[复制链接]
发表于 2022-4-30 16:10:29 | 显示全部楼层 |阅读模式

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

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

x
  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #define initsize 10

  4. struct list{
  5.         int *data;
  6.         int maxsize;
  7.         int length;
  8. };

  9. //初始化顺序表
  10. void Init_List(struct list *SeqList)
  11. {
  12.         SeqList->data = (int *)malloc(sizeof(int)*initsize);
  13.         if(SeqList->data)
  14.         {
  15.                 SeqList->length = 0;
  16.                 SeqList->maxsize = initsize;
  17.                 int i;
  18.                 for(i = 0; i < SeqList->maxsize; i++)
  19.                         SeqList->data[i] = 0;
  20.         }
  21. }


  22. //将顺序表填满
  23. void Insert_Data(struct list *SeqList)
  24. {
  25.         int i;
  26.         for(i = 0; i < SeqList->maxsize; i++)
  27.                 SeqList->data[i] = 1;
  28.         SeqList->length = SeqList->maxsize;
  29. }

  30. //给顺序表扩容
  31. struct list *Increase_space(struct list *SeqList, int len)
  32. {
  33.         int i;
  34.         //重新开辟堆空间
  35.         struct list *new_SeqList = NULL;
  36.         printf("%d\n", __LINE__);
  37.         new_SeqList->data = (int *)malloc(sizeof(int)*(SeqList->maxsize + len));
  38.         new_SeqList->maxsize = SeqList->maxsize + len;
  39.         new_SeqList->length = SeqList->maxsize;       
  40.         for(i = 0; i < new_SeqList->length; i++)
  41.                 new_SeqList->data[i] = SeqList->data[i];
  42.         //释放掉原堆空间
  43.         free(SeqList->data);
  44. }

  45. //打印顺序表中的数据
  46. void print_SeqList(struct list *SeqList)
  47. {
  48.         int i;
  49.         for(i = 0; i < SeqList->length; i++)
  50.                 printf("%d\n", SeqList->data[i]);
  51. }

  52. int main()
  53. {
  54.         struct list SeqList;
  55.         Init_List(&SeqList);
  56.         Insert_Data(&SeqList);
  57.         struct list *new_SeqList = Increase_space(&SeqList, 5);
  58.         print_SeqList(new_SeqList);
  59.         return 0;
  60. }
复制代码
最佳答案
2022-5-1 15:05:08
jackz007 发表于 2022-5-1 14:59
贴出错误信息看看
  1. $ cat main.c
  2. #include <stdio.h>
  3. #include <malloc.h>
  4. #define initsize 10

  5. struct list{
  6.         int *data;
  7.         int maxsize;
  8.         int length;
  9. };

  10. //初始化顺序表
  11. void Init_List(struct list *SeqList)
  12. {
  13.         SeqList->data = (int *)malloc(sizeof(int)*initsize);
  14.         if(SeqList->data)
  15.         {
  16.                 SeqList->length = 0;
  17.                 SeqList->maxsize = initsize;
  18.                 int i;
  19.                 for(i = 0; i < SeqList->maxsize; i++)
  20.                         SeqList->data[i] = 0;
  21.         }
  22. }


  23. //将顺序表填满
  24. void Insert_Data(struct list *SeqList)
  25. {
  26.         int i;
  27.         for(i = 0; i < SeqList->maxsize; i++)
  28.                 SeqList->data[i] = 1;
  29.         SeqList->length = SeqList->maxsize;
  30. }

  31. //给顺序表扩容
  32. struct list *Increase_space(struct list *SeqList, int len)
  33. {
  34.         int i;
  35.         //重新开辟堆空间
  36.         struct list *new_SeqList = NULL;
  37.         printf("%d\n", __LINE__);
  38.         new_SeqList->data = (int *)malloc(sizeof(int)*(SeqList->maxsize + len));
  39.         new_SeqList->maxsize = SeqList->maxsize + len;
  40.         new_SeqList->length = SeqList->maxsize;        
  41.         for(i = 0; i < new_SeqList->length; i++)
  42.                 new_SeqList->data[i] = SeqList->data[i];
  43.         //释放掉原堆空间
  44.         free(SeqList->data);
  45. }

  46. //打印顺序表中的数据
  47. void print_SeqList(struct list *SeqList)
  48. {
  49.         int i;
  50.         for(i = 0; i < SeqList->length; i++)
  51.                 printf("%d\n", SeqList->data[i]);
  52. }

  53. int main()
  54. {
  55.         struct list SeqList;
  56.         Init_List(&SeqList);
  57.         Insert_Data(&SeqList);
  58.         struct list *new_SeqList = Increase_space(&SeqList, 5);
  59.         print_SeqList(new_SeqList);
  60.         return 0;
  61. }
  62. $ gcc-debug -o main main.c
  63. main.c: In function ‘Increase_space’:
  64. main.c:49:1: warning: control reaches end of non-void function [-Wreturn-type]
  65.    49 | }
  66.       | ^
  67. $ ./main
  68. 41
  69. main.c:42:27: runtime error: member access within null pointer of type 'struct list'
  70. AddressSanitizer:DEADLYSIGNAL
  71. =================================================================
  72. ==31965==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000000 (pc 0x561b6d9cf9a1 bp 0x7ffc485f7b20 sp 0x7ffc485f7ae0 T0)
  73. ==31965==The signal is caused by a WRITE memory access.
  74. ==31965==Hint: address points to the zero page.
  75.     #0 0x561b6d9cf9a1 in Increase_space /tmp/main.c:42
  76.     #1 0x561b6d9d0134 in main /tmp/main.c:64
  77.     #2 0x7f334644c30f in __libc_start_call_main (/usr/lib/libc.so.6+0x2d30f)
  78.     #3 0x7f334644c3c0 in __libc_start_main@GLIBC_2.2.5 (/usr/lib/libc.so.6+0x2d3c0)
  79.     #4 0x561b6d9cf154 in _start (/tmp/main+0x3154)

  80. AddressSanitizer can not provide additional info.
  81. SUMMARY: AddressSanitizer: SEGV /tmp/main.c:42 in Increase_space
  82. ==31965==ABORTING
  83. $
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-4-30 20:21:09 | 显示全部楼层
     42 行有段错误?在我这里是正常编译、运行,编译器使用的是 VC17.1.5(VC 2022)及 gcc 11.2.0 Windows 版,除了在编译时有警告信息外,运行时一切正常。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-5-1 14:45:53 | 显示全部楼层
jackz007 发表于 2022-4-30 20:21
42 行有段错误?在我这里是正常编译、运行,编译器使用的是 VC17.1.5(VC 2022)及 gcc 11.2.0 Window ...

我是在 Linux 环境下,用 gcc 编译的,确实无法编译。。。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-5-1 14:59:47 | 显示全部楼层
她的睫毛 发表于 2022-5-1 14:45
我是在 Linux 环境下,用 gcc 编译的,确实无法编译。。。

      贴出错误信息看看
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-5-1 15:05:08 | 显示全部楼层    本楼为最佳答案   
jackz007 发表于 2022-5-1 14:59
贴出错误信息看看
  1. $ cat main.c
  2. #include <stdio.h>
  3. #include <malloc.h>
  4. #define initsize 10

  5. struct list{
  6.         int *data;
  7.         int maxsize;
  8.         int length;
  9. };

  10. //初始化顺序表
  11. void Init_List(struct list *SeqList)
  12. {
  13.         SeqList->data = (int *)malloc(sizeof(int)*initsize);
  14.         if(SeqList->data)
  15.         {
  16.                 SeqList->length = 0;
  17.                 SeqList->maxsize = initsize;
  18.                 int i;
  19.                 for(i = 0; i < SeqList->maxsize; i++)
  20.                         SeqList->data[i] = 0;
  21.         }
  22. }


  23. //将顺序表填满
  24. void Insert_Data(struct list *SeqList)
  25. {
  26.         int i;
  27.         for(i = 0; i < SeqList->maxsize; i++)
  28.                 SeqList->data[i] = 1;
  29.         SeqList->length = SeqList->maxsize;
  30. }

  31. //给顺序表扩容
  32. struct list *Increase_space(struct list *SeqList, int len)
  33. {
  34.         int i;
  35.         //重新开辟堆空间
  36.         struct list *new_SeqList = NULL;
  37.         printf("%d\n", __LINE__);
  38.         new_SeqList->data = (int *)malloc(sizeof(int)*(SeqList->maxsize + len));
  39.         new_SeqList->maxsize = SeqList->maxsize + len;
  40.         new_SeqList->length = SeqList->maxsize;        
  41.         for(i = 0; i < new_SeqList->length; i++)
  42.                 new_SeqList->data[i] = SeqList->data[i];
  43.         //释放掉原堆空间
  44.         free(SeqList->data);
  45. }

  46. //打印顺序表中的数据
  47. void print_SeqList(struct list *SeqList)
  48. {
  49.         int i;
  50.         for(i = 0; i < SeqList->length; i++)
  51.                 printf("%d\n", SeqList->data[i]);
  52. }

  53. int main()
  54. {
  55.         struct list SeqList;
  56.         Init_List(&SeqList);
  57.         Insert_Data(&SeqList);
  58.         struct list *new_SeqList = Increase_space(&SeqList, 5);
  59.         print_SeqList(new_SeqList);
  60.         return 0;
  61. }
  62. $ gcc-debug -o main main.c
  63. main.c: In function ‘Increase_space’:
  64. main.c:49:1: warning: control reaches end of non-void function [-Wreturn-type]
  65.    49 | }
  66.       | ^
  67. $ ./main
  68. 41
  69. main.c:42:27: runtime error: member access within null pointer of type 'struct list'
  70. AddressSanitizer:DEADLYSIGNAL
  71. =================================================================
  72. ==31965==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000000 (pc 0x561b6d9cf9a1 bp 0x7ffc485f7b20 sp 0x7ffc485f7ae0 T0)
  73. ==31965==The signal is caused by a WRITE memory access.
  74. ==31965==Hint: address points to the zero page.
  75.     #0 0x561b6d9cf9a1 in Increase_space /tmp/main.c:42
  76.     #1 0x561b6d9d0134 in main /tmp/main.c:64
  77.     #2 0x7f334644c30f in __libc_start_call_main (/usr/lib/libc.so.6+0x2d30f)
  78.     #3 0x7f334644c3c0 in __libc_start_main@GLIBC_2.2.5 (/usr/lib/libc.so.6+0x2d3c0)
  79.     #4 0x561b6d9cf154 in _start (/tmp/main+0x3154)

  80. AddressSanitizer can not provide additional info.
  81. SUMMARY: AddressSanitizer: SEGV /tmp/main.c:42 in Increase_space
  82. ==31965==ABORTING
  83. $
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-5-2 02:20:12 | 显示全部楼层
jackz007 发表于 2022-5-1 14:59
贴出错误信息看看

41 行这里我设置了一个 NULL 指针,所以后面就报错了。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-24 10:28

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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