You should deduced that transitively since '\0' == 0Originally Posted by P3nGu1N
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
you made a very valid point... I'm really just trying to destroy each list item in an array of lists, which means I should only have to access each Contents[i] once provided it wasn't NULL to begin with, then set my pointer p to the beginning of the list only in that senario, which could be done with an if statement. Changed code to:
and no more infinite loop. I just hope it does what I want.Code:void DestroySymTab(struct SymTab *ATable) { int i; struct SymEntry *p; for (i = 0; i < ATable->Size; i++) { if(ATable->Contents[i] != NULL) { p = (struct SymEntry*)ATable->Contents[i]; while(p->Next != NULL){ p = p->Next; } free(p); p = NULL; } } free(ATable); };
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
The sad thing is I've already created this program in Java last year. It's much easier for me to look at abstract data types like chain bucket hashing from an object oriented language.
Ok, so the new code only has one slight problem: You probably want to destroy EVERY element in the linked list, not just the last one.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
It is fine to use the code you have now - the code you posted that was printing stuff, you never set p to anything.
Your latest code looks OK, except that you only free the last pointer of the linked list - which is probably not what you want to do. Note that you will need a second pointer to hold the next pointer when you delete the current one.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
I see a problem!
What you are doing there is looping thru until p->Next==NULL, then you free p it and set it to NULL. Meaning the last node, not all of them. To do what you want to do I think you will need a tmp pointer:
Code:while(p->Next != NULL){ tmp=p; p = p->Next; free(tmp); tmp=NULL; /* the previous ones */ } free(p); p=NULL; /* the last one */
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
That's exactly what you want to do -- set p to point to each location you want to free and then free it. Currently you are setting p to nothing in particular, since you commented out any and every assignment involving p. Your original mistake was thinking that if you did something like this:
that foo was later 5 (i.e., you assumed that contents[i] changed because you changed p). Assigning a pointer doesn't bind a reference, it just stores that address.Code:struct whatever *p = &foo; p = 5;
A point to remember is that you can free in any order; i.e. you can do something like:
Code:while (next_to_free) is not NULL to_be_freed = next_to_free; next_to_free = next_to_free->next; free(to_be_freed)
Or this:
I removed the "tmp = NULL" and "p = NULL", as if you us correct scopign for them, it shouldn't be necessary to set them to NULL to avoid invalid access.Code:while(p != NULL){ tmp=p; p = p->Next; free(tmp); }
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Yeah, I thought about that -- since you have no way of accessing the pointers now, it doesn't matter if they are NULL, and they have been freed.
However, if you are just starting out with C I would say setting a pointer to NULL after a free() is a good habit to get into now, as in other circumstances it makes it easier to avoid a double free
Code:if (ptr) free (ptr);
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge