Search code examples
cdata-structuresvoid-pointersmemmove

Generic stack push error


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

Solution

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