
Bonehead malloc/free question
Quote:
>For example, if I have a linked list pointed to with ll_ptr, will this
>work o.k.?
>while(ll_ptr != NULL) {
> free((char *)ll_ptr);
> ll_ptr = ll_ptr->next;
>}
>2 troubling points:
>(1) My compiler seems to force me to cast the pointer to type char *
>for free(char *ptr) to work, (go figure - char *ptr needs to be a char
>*).
Your compiler is astonishingly old from the sounds of it - or worse
its not a proper C compiler. Free takes a void* and this would need
no cast. Pre-ANSI compilers expected a char*. Since ll_ptr is not a
char*, _you_ need the cast. Get a new compiler
Quote:
>(2) pointing to the "next" after freeing the previous node seems weird.
What you're doing is illegal. You free the data, then access a member
of it. Blammo! Find the next element BEFORE freeing the pointer...
Quote:
>Are either of these points worth fretting over?
Yes
--
Mark McIntyre
C- FAQ: http://www.eskimo.com/~scs/C-faq/top.html