Sololearn: Learn to Code
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5
Those are two different designs. In the second case, it appears to be a list of integers. An int is usually a part of the struct and its memory available with the allocation of the struct. In the first case, the list appears to be generic (as generic as C can get). Since the list can hold any data type, the data js provided for on the heap and only the reference (pointer) is sored in the list (as void pointer to point to anything). In that case, memory for the data payload has to be allocated as well.
20th Feb 2022, 4:11 PM
Ani Jona 🕊
Ani Jona 🕊 - avatar
+ 3
Take a close look at the Node struct. I suspect it has a member void* data. When the struct is allocated, only a pointer size is reserved for data. But no actual memory to store what data is pointing to.
20th Feb 2022, 4:46 PM
Ani Jona 🕊
Ani Jona 🕊 - avatar
+ 1
I would use calloc if your not initializing the data to anything at the point of line 7. At least with calloc the bit blocks get initialized.
20th Feb 2022, 4:06 PM
William Owens
William Owens - avatar