鱼C论坛

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

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

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

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

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

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

struct list{
        int *data;
        int maxsize;
        int length;
};

//初始化顺序表
void Init_List(struct list *SeqList)
{
        SeqList->data = (int *)malloc(sizeof(int)*initsize);
        if(SeqList->data)
        {
                SeqList->length = 0;
                SeqList->maxsize = initsize;
                int i;
                for(i = 0; i < SeqList->maxsize; i++)
                        SeqList->data[i] = 0;
        }
}


//将顺序表填满
void Insert_Data(struct list *SeqList)
{
        int i;
        for(i = 0; i < SeqList->maxsize; i++)
                SeqList->data[i] = 1;
        SeqList->length = SeqList->maxsize;
}

//给顺序表扩容
struct list *Increase_space(struct list *SeqList, int len)
{
        int i;
        //重新开辟堆空间
        struct list *new_SeqList = NULL;
        printf("%d\n", __LINE__);
        new_SeqList->data = (int *)malloc(sizeof(int)*(SeqList->maxsize + len));
        new_SeqList->maxsize = SeqList->maxsize + len;
        new_SeqList->length = SeqList->maxsize;        
        for(i = 0; i < new_SeqList->length; i++)
                new_SeqList->data[i] = SeqList->data[i];
        //释放掉原堆空间
        free(SeqList->data);
}

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

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

struct list{
        int *data;
        int maxsize;
        int length;
};

//初始化顺序表
void Init_List(struct list *SeqList)
{
        SeqList->data = (int *)malloc(sizeof(int)*initsize);
        if(SeqList->data)
        {
                SeqList->length = 0;
                SeqList->maxsize = initsize;
                int i;
                for(i = 0; i < SeqList->maxsize; i++)
                        SeqList->data[i] = 0;
        }
}


//将顺序表填满
void Insert_Data(struct list *SeqList)
{
        int i;
        for(i = 0; i < SeqList->maxsize; i++)
                SeqList->data[i] = 1;
        SeqList->length = SeqList->maxsize;
}

//给顺序表扩容
struct list *Increase_space(struct list *SeqList, int len)
{
        int i;
        //重新开辟堆空间
        struct list *new_SeqList = NULL;
        printf("%d\n", __LINE__);
        new_SeqList->data = (int *)malloc(sizeof(int)*(SeqList->maxsize + len));
        new_SeqList->maxsize = SeqList->maxsize + len;
        new_SeqList->length = SeqList->maxsize;        
        for(i = 0; i < new_SeqList->length; i++)
                new_SeqList->data[i] = SeqList->data[i];
        //释放掉原堆空间
        free(SeqList->data);
}

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

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

AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV /tmp/main.c:42 in Increase_space
==31965==ABORTING
$ 
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-4-30 20:21:09 | 显示全部楼层
     42 行有段错误?在我这里是正常编译、运行,编译器使用的是 VC17.1.5(VC 2022)及 gcc 11.2.0 Windows 版,除了在编译时有警告信息外,运行时一切正常。
想知道小甲鱼最近在做啥?请访问 -> 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 编译的,确实无法编译。。。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

      贴出错误信息看看
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

struct list{
        int *data;
        int maxsize;
        int length;
};

//初始化顺序表
void Init_List(struct list *SeqList)
{
        SeqList->data = (int *)malloc(sizeof(int)*initsize);
        if(SeqList->data)
        {
                SeqList->length = 0;
                SeqList->maxsize = initsize;
                int i;
                for(i = 0; i < SeqList->maxsize; i++)
                        SeqList->data[i] = 0;
        }
}


//将顺序表填满
void Insert_Data(struct list *SeqList)
{
        int i;
        for(i = 0; i < SeqList->maxsize; i++)
                SeqList->data[i] = 1;
        SeqList->length = SeqList->maxsize;
}

//给顺序表扩容
struct list *Increase_space(struct list *SeqList, int len)
{
        int i;
        //重新开辟堆空间
        struct list *new_SeqList = NULL;
        printf("%d\n", __LINE__);
        new_SeqList->data = (int *)malloc(sizeof(int)*(SeqList->maxsize + len));
        new_SeqList->maxsize = SeqList->maxsize + len;
        new_SeqList->length = SeqList->maxsize;        
        for(i = 0; i < new_SeqList->length; i++)
                new_SeqList->data[i] = SeqList->data[i];
        //释放掉原堆空间
        free(SeqList->data);
}

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

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

AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV /tmp/main.c:42 in Increase_space
==31965==ABORTING
$ 
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

41 行这里我设置了一个 NULL 指针,所以后面就报错了。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-10-5 22:28

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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