0
votes

I have one structure that contains pointer to another structure Node. This pointer is a front pointer to the struct Linked List. So, I am building my link list and each time I insert node I create separate structure and link in to other LL nodes.

Question: do I need to allocate memory for each node in LL? Meaning using malloc such as *pointer_to_struct = (structAlias *)malloc(sizeof(structAlias)); and then to initialize its members.

Or I simply create structure and give values to it's members (members are void * and structAlias *next) without memory allocating for each struct?

1
Unless, you want to save your link list in a file as an object, you do not need to allocate memory for structure.Keshab
If you don't want to save any information in linked list then why are you creating it?ani627

1 Answers

3
votes

In the general case, yes, you must allocate memory for each node in the list, and you will probably want to use either malloc or calloc. If you just declare a struct local variable, that local variable will be invalidated when the function returns, but you probably want the node to outlive the function call.