question on free function
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > question on free function




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    question on free function  
jamesonang@gmail.com


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-12-06 10:51 PM

i have some doubts on free function when i write a linked list.

here are my data structure:

typedef struct  listnode {
other_datatype    *data;
struct listnode     *next ;

} listnode_t ;

listnode_t    *listhead;
...
...

I write a freelist ( ) function to free memory when linked list is not
used. I want to free all memory ,  including the linked list self and
the data pointed by listnode_t.data.

1  void freelist () {
2  listnode_t  ptr =null ;
3  while( listhead!=null ) {
4        ptr = listhead;
5        listhead = listhead->next ;
6        free( ptr->data ) ;
7        free( ptr ) ;
8   }
9 }

why above code could not work?  when i remove the line 6 , compiler
report no error.
but the memory of ptr->data  was freed ? why?

thanks for help!






[ Post a follow-up to this message ]



    Re: question on free function  
Nils O. Selåsdal


Report This Message To A Moderator Edit/Delete Message


 
03-12-06 10:51 PM

jamesonang@gmail.com wrote:
> i have some doubts on free function when i write a linked list.
>
> here are my data structure:
>
> typedef struct  listnode {
>     other_datatype    *data;
>     struct listnode     *next ;
>
> } listnode_t ;
>
> listnode_t    *listhead;
> ...
> ...
>
> I write a freelist ( ) function to free memory when linked list is not
> used. I want to free all memory ,  including the linked list self and
> the data pointed by listnode_t.data.
>
> 1  void freelist () {
> 2  listnode_t  ptr =null ;
given the above declarations, the above should read
listnode_t *ptr = NULL;

Please post actual code.

> 3  while( listhead!=null ) {
> 4        ptr = listhead;
> 5        listhead = listhead->next ;
> 6        free( ptr->data ) ;
> 7        free( ptr ) ;
> 8   }
> 9 }
>
> why above code could not work?  when i remove the line 6 , compiler
> report no error.
Not that easy to guess. It should work.
What is the exact error the compiler tells you ?

> but the memory of ptr->data  was freed ? why?

What ?


> thanks for help!
>





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 02:59 AM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register