1

我试图创建一个通用堆栈。当我尝试将值压入堆栈时遇到问题,程序在 memmove 行中崩溃:

typedef struct s_node{
    void *info;
    struct s_node *next;
}t_node;

typedef struct{
    char name[50];
    int salary;
}t_employee;

typedef t_node* t_stack;

void createStack(t_stack *p){
    *p=NULL;
}

int push(t_stack *p,void *inf,int siz){
    t_node *new=(t_node*)malloc(sizeof(t_node));
    if(!new)return 0;
    memmove(new->info,inf,siz);    !!!!!CRASH
    new->next=*p;
    *p=new;
    return 1;
}

int main()
{
    t_stack p;
    t_employee e={"Jhon Freeman",30000};
    createStack(&p);
    push(&p,&e,sizeof(t_employee));
    return 0;
}
4

2 回答 2

1

new->info 没有指向任何地方。初始化它:)

于 2017-05-23T01:20:43.310 回答
1

你声明了 new,但是 new->info 没有被初始化。

于 2017-05-23T01:31:32.040 回答