在C中实现堆栈时的Segfault

在C中实现堆栈时的Segfault,c,struct,segmentation-fault,stack,C,Struct,Segmentation Fault,Stack,我在运行这段代码以实现C中的堆栈时遇到一个segfault。请注意,这段代码有点不完整。我只是想检查一下,看看是否可以将一些元素推到堆栈上并打印出来。但它又抛出了一个错误。任何帮助都将不胜感激 #include<stdlib.h> #include<stdio.h> struct stack { int *elems; int ll; int al; }; void stack_new(struct stack *s) { s->ll=0; s->al

我在运行这段代码以实现C中的堆栈时遇到一个segfault。请注意,这段代码有点不完整。我只是想检查一下,看看是否可以将一些元素推到堆栈上并打印出来。但它又抛出了一个错误。任何帮助都将不胜感激

#include<stdlib.h>
#include<stdio.h>

struct stack
{

int *elems;
int ll;
int al;
};

void stack_new(struct stack *s)

{
s->ll=0;
s->al=4;
s->elems=malloc(4*sizeof(int));
}

void stack_del(struct stack *s)
{
free(s->elems);
}

void stack_push(struct stack *s,int value)
{
if(s->ll==s->al)
{
printf("overflow");
/*s->al*=2;
s->elems=realloc(s->elems, s->al*sizeof(int));*/
}

s->elems[s->ll]=value;
s->ll++;
}

void stack_pop(struct stack *s)
{
s->ll--;
return (s->elems[s->ll]);
}

void main()
{
struct stack *s;

stack_new(s);
stack_push(s,3);
stack_push(s,4);
stack_push(s,8);

printf("%d", s->elems[0]);

//stack_pop(s);
//stack_del(s);
}
#包括
#包括
结构堆栈
{
int*元素;
国际劳工组织;
国际;
};
void stack_new(结构堆栈*s)
{
s->ll=0;
s->al=4;
s->elems=malloc(4*sizeof(int));
}
void stack_del(结构堆栈*s)
{
免费(s->elems);
}
void stack_push(结构堆栈*s,int值)
{
如果(s->ll==s->al)
{
printf(“溢出”);
/*s->al*=2;
s->elems=realloc(s->elems,s->al*sizeof(int))*/
}
s->elems[s->ll]=值;
s->ll++;
}
void stack_pop(结构堆栈*s)
{
s->ll--;
返回(s->elems[s->ll]);
}
void main()
{
结构堆栈*s;
堆栈_新(s);
堆叠推送(s,3);
堆叠推送(s,4);
堆栈推送(s,8);
printf(“%d”,s->elems[0]);
//堆垛(s);;
//烟囱(s);;
}
声明

struct stack *s;
不为结构堆栈分配任何内存。这样做:

struct stack *s = malloc(sizeof *s);
或者只需将堆栈放在堆栈上:

struct stack s;

stack_new(&s);
…

使用更具描述性的字段名也是一个好主意。

您有几个错误

  • 您从未在
    main
    函数中初始化指针
    s
    ,因此在
    stack\u new
    函数中,取消引用
    s
    会导致分段错误

  • 您应该首先为堆栈分配空间,无论您想在哪里,但必须在哪里

  • 另一件事是,如果您想用一个常量初始化
    al
    字段,然后分配一个大小不变的数组,则不需要该字段
    al
    ,您可以将
    elems
    声明为
    int elems[constant\u number]
    ,但如果您希望它是动态的,这就是我认为您希望从检查中得到的
    如果在
    stack\u push
    函数中(s->ll==s->al)
    ,那么您只需将希望
    al
    必须的值传递给
    stack\u new
    函数即可

  • 这是您的一些代码,已修复,因此您可以了解我的实际意思

    #include<stdlib.h>
    #include<stdio.h>
    
    struct stack
    {
        int *elems;
        int  ll;
        int  al;
    };
    
    struct stack *stack_new(int al) /* you can pass the maximum number of elements allowed */
    {
        struct stack *s;
        s = malloc(sizeof(struct stack));
        if (s == NULL)
            return NULL;
        s->ll    = 0;
        s->al    = al;
        s->elems = malloc(al * sizeof(int)); /* and you dynamically allocate space for them here */
    
        return s;
    }
    
    void stack_del(struct stack *s)
    {
        if (s != NULL) /* always check the pointers to prevent `SEGMENTATION FAULT` */
        {
            if (s->elems != NULL)
                free(s->elems);
            free(s);
        }
    }
    
    void stack_push(struct stack *s, int value)
    {
        if (s == NULL)
            return;
        if(s->ll == s->al)
        {
            printf("overflow");
            /*s->al*=2;
            s->elems=realloc(s->elems, s->al*sizeof(int));*/
        }
        if (s->elems != NULL)
            s->elems[s->ll] = value;
        s->ll++;
    }
    
    int stack_pop(struct stack *s)
    {
        if ((s == NULL) || (s->elems == NULL))
            return 0;
        s->ll--;
        return (s->elems[s->ll]);
    }
    
    int main()
    {
        struct stack *s;
    
        s = stack_new(4);
        stack_push(s, 3);
        stack_push(s, 4);
        stack_push(s, 8);
    
        printf("%d", s->elems[0]);
    
        stack_pop(s);
        stack_del(s);
    
        return 0;
    }
    
    #包括
    #包括
    结构堆栈
    {
    int*元素;
    国际劳工组织;
    国际;
    };
    struct stack*stack_new(int al)/*您可以传递允许的最大元素数*/
    {
    结构堆栈*s;
    s=malloc(sizeof(struct stack));
    如果(s==NULL)
    返回NULL;
    s->ll=0;
    s->al=al;
    s->elems=malloc(al*sizeof(int));/*并在此处为它们动态分配空间*/
    返回s;
    }
    void stack_del(结构堆栈*s)
    {
    如果(s!=NULL)/*始终检查指针以防止“分段错误”*/
    {
    如果(s->elems!=NULL)
    免费(s->elems);
    免费的;
    }
    }
    void stack_push(结构堆栈*s,int值)
    {
    如果(s==NULL)
    返回;
    如果(s->ll==s->al)
    {
    printf(“溢出”);
    /*s->al*=2;
    s->elems=realloc(s->elems,s->al*sizeof(int))*/
    }
    如果(s->elems!=NULL)
    s->elems[s->ll]=值;
    s->ll++;
    }
    int stack_pop(结构堆栈*s)
    {
    if((s==NULL)| |(s->elems==NULL))
    返回0;
    s->ll--;
    返回(s->elems[s->ll]);
    }
    int main()
    {
    结构堆栈*s;
    s=新堆栈(4);
    堆叠推送(s,3);
    堆叠推送(s,4);
    堆栈推送(s,8);
    printf(“%d”,s->elems[0]);
    堆垛(s);;
    烟囱(s);;
    返回0;
    }
    
    ```