1

Im trying to create a generic stack. I have a problem when i try to push a value into the stack, the programm crashes in the memmove line:

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;
}

2 Answers2

1

new->info is pointing to nowhere. Initialize it :)

Gam
  • 684
  • 1
  • 8
  • 17
1

You declare new, but new->info is not initialized.