sophiakoulen / minishell

A simplified bash-like shell, with pipes, redirections and variable expansion.
4 stars 1 forks source link

is this wrong? #143

Open znichola opened 1 year ago

znichola commented 1 year ago
t_item  *item_factory(t_item *blueprint)
{
    t_item  *item;

    item = x_malloc(sizeof(*item), 1);

it's a bit late, I can't tell if this is wrong so putting it here for later. It feels like it should be a sizeof(item)

sophiakoulen commented 1 year ago

No, it's not wrong. In case of doubt, it's good practice to assign in the form ptr = malloc(sizeof(*ptr));. The size of the allocation is the size of the data type the pointer points to, not of the pointer itself (which is always 8 bytes).

znichola commented 1 year ago

Dosen't sizeof(*ptr) mean it's the size of a pointer and not the data type, I had some doubts when I thought about how we malloc for a string str = (char *)malloc(sizeof(char) * len)

sophiakoulen commented 1 year ago

ptr is what ptr points to. So if ptr is of type `t_list , then *ptr is of typet_list`.