c - how to correctly initialize a LinkList -
When launching a link list, I define the parameter as * L, such as:
Status InitList (LinkList * L) {* L = (LinkList) Molec (form (structure LNode)); If (! * L) exit (overflow); (* L) - & gt; Next = zero; Okay; } but not
position InitList (LinkList L) {l = (LinkList) malloc (structure LNode); If (! L) exit (overflow); (L) - & gt; Next = zero; Okay; } Why can not this be true? struct LNode {ElemType data; Structure LNode * Next; }; Typingfile structure LNode * link list;
In another implementation, you have a local variable L You have trouble getting started, this is a local variable ??? Changing this will not change the variable in the calling code. When it comes out of the ceremony, then the memory you have allotted has lost it ??? Memory leak they are bad! The first code gets an indicator on the variable in the calling code and carefully changes it. It does not leak to memory.
You can modify the other, which makes it work by using the code:
link-list newlive (zero) {linklist l = (linklist) malloc (sizeof (* L)); If (L) {L-> data = 0; L- & gt; Next = null; } Return L; } It does a different job, hence its name was changed. Note that it ensures that all elements of the structure are initialized for known values. You call it this like this:
linklist list = newlive ();
Comments
Post a Comment