Bonehead malloc/free question 
Author Message
 Bonehead malloc/free question

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;

Quote:
}

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
*).

(2)  pointing to the "next" after freeing the previous node seems weird.

Are either of these points worth fretting over?

Thanks for any input.

Sent via Deja.com http://www.*-*-*.com/
Before you buy.



Tue, 31 Dec 2002 03:00:00 GMT  
 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
> *).

> (2)  pointing to the "next" after freeing the previous node seems weird.

> Are either of these points worth fretting over?

> Thanks for any input.

> Sent via Deja.com http://www.deja.com/
> Before you buy.

1. ll_ptr is obviously not a char * so it has to be cast (if your compiler
options are very picky, e.g. C++ mode)
2. it's not just weird, it's wrong and should be changed (keep a temporary
copy of ll_ptr->next before freeing ll_ptr)


Tue, 31 Dec 2002 03:00:00 GMT  
 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


Tue, 31 Dec 2002 03:00:00 GMT  
 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;
> }

No.

Quote:
> 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
> *).

Have you remembered to #include <stdlib.h>? I can't think of any other
decent reason to complain about that.

Quote:
> (2)  pointing to the "next" after freeing the previous node seems weird.

And is. You're invoking undefined behaviour; this _may_ work, but only
by accident. The computer is quite at liberty to dump core on you from
twelve feet high if you try this. You should, as you suspected, never
use memory after you've freed it.
Try this one:

  while (ll_ptr) {
    temp_ptr=llptr->next;
    free(ll_ptr);
    ll_ptr=temp_ptr;
  }

Richard



Wed, 01 Jan 2003 03:00:00 GMT  
 Bonehead malloc/free question
[...]

Quote:
> (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
> *).

Do you have a #include <stdlib.h>?

--

San Diego Supercomputer Center           <*>  <http://www.sdsc.edu/~kst>
Welcome to the last year of the 20th century.



Wed, 01 Jan 2003 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. malloc, realloc, free questions

2. malloc/free questions

3. malloc - free question

4. malloc/free question

5. Dumb question concerning malloc/free

6. a question about malloc and free

7. simple question about malloc & free

8. basic malloc/free question

9. dumb malloc free question

10. malloc/free question with Purify

11. malloc(), free(), and strtok() questions

12. Intersting malloc() / free() question

 

 
Powered by phpBB® Forum Software