When using the NewList() function described in exec/lists.h (at least in SAS/C 6.58), does one need to allocate memory for a new list separately? What does NewList() do exactly?
NewList() does not care where the memory came from, but you need to provide enough memory to hold a list structure. Certainly, the lifetime of the memory must be at least as long as you wish to keep the list around.
That is, if you need the list only in a particular function, and the function called from there, a "struct List" on the stack will certainly do, once initalized correctly. If, however, you need to access the list after the function that initialized it returned, you'd better allocate it from the heap, or place it outside of a function.
NewList() doesn't do particularly much, which is why exec traditionally doesn't include a function for it (amiga.lib does):
void NewList(struct List *lh)
{
lh->lh_Head = (struct Node *)&(lh->lh_Tail);
lh->lh_Tail = NULL
lh->lh_TailPred = (struct Node *)&(lh->lh_Head);
}
[CODE]
/* Is NewList(struct List *) the same as the following? */
struct List *list;
list = (struct List *)AllocMem(sizeof(struct List), MEMF_ANY);
memset(list, 0L, sizeof(struct List));
No. NewList() doesn't allocate memory, and setting all elements to zero is not appropriate to initialize a list. I gave a possible implementation above.
/* Or does it do some magic black box stuff? And if I want to pass
my list around, should I malloc() the memory for the list and then
remember to free it myself later; after passing it around */
Whether you malloc the list or not, i.e. whether you place the object on the stack or the heap actually depends on the very same question as for every other C object: It is a matter of the lifetime of the object. An object on the stack deceases as soon as you leave the function within which it was created. If that lifetime is too short, you need to take the memory from the heap, i.e. malloc() it.