Unix Programming - question on free function

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > March 2006 > question on free function





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author question on free function
jamesonang@gmail.com

2006-03-12, 7:47 am

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

here are my data structre:

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 reome the line 6 , compiler
report no error.
but the memory of ptr->data was freed ? why?

thanks for help!

Thomas Maier-Komor

2006-03-12, 5:51 pm

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

jamesonang@gmail.com wrote:
> i have some doubts on free function when i write a linked list.
>
> here are my data structre:
>
> 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 reome the line 6 , compiler
> report no error.
> but the memory of ptr->data was freed ? why?
>
> thanks for help!
>


accessing a block of memory after free'ing it, results in undefined
behavior. I.e. it could produce the value it held before, but it could
also crash your program (read SIGSEGV) or do something totally different...

BTW: free'ing memory that was never malloc'ed results in undefined
behavior, too.

HTH,
Tom
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (SunOS)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFEFCw46U+hp8PKQZIRAunpAKD2EPdyuyA9
9C1+F925R1gF5koxLgCgiclJ
/hUTkr0046yOeZk/P7z2T10=
=U6p1
-----END PGP SIGNATURE-----
Logan Shaw

2006-03-12, 5:51 pm

Thomas Maier-Komor wrote:
> jamesonang@gmail.com wrote:


[vbcol=seagreen]
> accessing a block of memory after free'ing it, results in undefined
> behavior.


True, but I can't see a place in the above code where free()d memory
is accessed. The code shows ptr->data being free()d before ptr is,
and it also shows listhead->next being read before the node is free()d,
both of which are the correct order.

- Logan
Thomas Maier-Komor

2006-03-12, 5:51 pm

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Logan Shaw wrote:
> Thomas Maier-Komor wrote:
>
>
>
> True, but I can't see a place in the above code where free()d memory
> is accessed. The code shows ptr->data being free()d before ptr is,
> and it also shows listhead->next being read before the node is free()d,
> both of which are the correct order.
>
> - Logan


yes I know that this is not included in the source code. I extracted it
from the prose ;-)

- From the OP's text I assumed that he tried to access an element after
calling his freelist() and wondered why he was still able to access the
data. In my experience this situation causes a frown for almost every
person who encounters it the first time...

To the OP: if my answer did not help you solve your problem, tell us a
little bit more. I.e. we need more source code that is actually
compilable to fully understand what your problem is and what's its cause.

Tom
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (SunOS)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFEFHBu6U+hp8PKQZIRAsKfAKCE7f4LEdFN
efcumeChgTUtcUkPEwCcDitj
1lFrj18aaacy4KmldNWprsM=
=gxyG
-----END PGP SIGNATURE-----
jamesonang@gmail.com

2006-03-12, 8:47 pm

thanks your response.

i create a linked list to organize the some data (namely
other_datatype).

firstly , i create the linked list , some util function to access it,
finally i want to free all memory i use malloc() create.

some source code ,here. ( for short, i ommit some code )

/* list.c */
/* define the datatype*/
typedef struct pdata {
char name[8] ;
int age;
}other_datatype ; /*the definiton of other_datatype is just a
example*/

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

listnode_t *listhead;
listnode_t *newnode ;
listnode_t *ptr;

/*now create a newnode*/
newnode = (listnode_t *) malloc ( sizeof(listnode_t) ) ;
newnode->data = (other_datatype *)malloc ( sizeof(other_datatype) );
newnode->data->age = 10;
strcpy(newnode->data->name,"test") ;
newnode->next = NULL;

/*add to list*/
if (listhead ==NULL)
listhead = newnode;
else {
newnode->next = listhead->next;
listhead = newnode ;
}

/*after add some node to list , i want to free them. so i first free
the other_datatype pointed by newnode->data ,then free the list self
pointed by newnode . does i make my idea clear? english is not my
native language ,sorry it */

the freelist () function like the code had posted
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 }

what's wrong with it (the freelist function)? the compiler report
error, i remove the line 6 there no error reported!

if i do not call free(ptr->data), are memory space occupied by the
actual data (other_datatype) freed ?

bye the way , why say i access memory after free it ?
thanks for your help!

Pascal Bourguignon

2006-03-13, 2:48 am

"jamesonang@gmail.com" <jamesonang@gmail.com> writes:

> thanks your response.
>
> i create a linked list to organize the some data (namely
> other_datatype).
>
> firstly , i create the linked list , some util function to access it,
> finally i want to free all memory i use malloc() create.
>
> some source code ,here. ( for short, i ommit some code )


No, you need to provide the whole program! There's absolutely nothing
wrong with your freelist function. The problem is elsewhere. If you
don't provide the whole source, we won't be able to debug it for you.
So you'll have to debug it yourself.

> what's wrong with it (the freelist function)? the compiler report
> error, i remove the line 6 there no error reported!


Would that kill you to copy-and-paste the error message?





Well, there's one thing that's wrong with your code, but it's not
relevant to your problem. It would be better not to have global
variables, and to pass as argument to freelist the list you want to
free.

Also, I'd use different naming conventions, using _ to separate words
in identifiers.

I would write it as:

void list_node_free(list_node_t* node){
free(node->data);
free(node);}

void list_free(list_node_t* list){
while(list!=null){
list_node_t* next=list->next;
list_node_free(list);
list=next;}}


--
__Pascal Bourguignon__ http://www.informatimago.com/

"Debugging? Klingons do not debug! Our software does not coddle the
weak."
Måns Rullgård

2006-03-13, 2:48 am

Pascal Bourguignon <usenet@informatimago.com> writes:

> list=next;}}


This is supposed to be C, not Lisp or some fancy smiley.

--
Måns Rullgård
mru@inprovide.com
jamesonang@gmail.com

2006-03-13, 2:48 am

i just want to know :

if i do not call free(ptr->data) before calling free(ptr), are memory
space occupied by the
actual data (other_datatype) freed ? ptr->data is also a pointer to
some various created by malloc(). (please see definition of data type)

that's my question. other code has nothing to with this question, i
think.

Pascal Bourguignon

2006-03-13, 2:48 am

"jamesonang@gmail.com" <jamesonang@gmail.com> writes:

> i just want to know :
>
> if i do not call free(ptr->data) before calling free(ptr), are memory
> space occupied by the
> actual data (other_datatype) freed ?


Of course not.

> ptr->data is also a pointer to
> some various created by malloc(). (please see definition of data type)
>
> that's my question. other code has nothing to with this question, i
> think.



--
__Pascal Bourguignon__ http://www.informatimago.com/

"Debugging? Klingons do not debug! Our software does not coddle the
weak."
Gordon Burditt

2006-03-13, 2:48 am

>if i do not call free(ptr->data) before calling free(ptr), are memory
>space occupied by the
>actual data (other_datatype) freed ?


NO!

one malloc(), one free().

If you want to free everything, you need to call free() exactly
once for each time you called malloc() successfully, with appropriate
adjustments if you used realloc() (which can act as malloc() or
free()). Oh, yes, free(NULL) doesn't count as a call to free(),
and calls to malloc() that return NULL also don't count.

>ptr->data is also a pointer to
>some various created by malloc(). (please see definition of data type)


free() does not look at the definition of data types. If you call
free(ptr), it does *NOT* go in and free(ptr->data) for you. I can
think of a number of applications where doing so would be disastrous.
One such situation would be if the various ptr->data pointers often
pointed at the *same* copy of the data, or sometimes pointed to
non-malloc()ed data.

>that's my question. other code has nothing to with this question, i
>think.


Gordon L. Burditt
Nils O. Selåsdal

2006-03-13, 2:48 am

jamesonang@gmail.com wrote:
> i just want to know :
>
> if i do not call free(ptr->data) before calling free(ptr), are memory
> space occupied by the
> actual data (other_datatype) freed ? ptr->data is also a pointer to
> some various created by malloc(). (please see definition of data type)
>
> that's my question. other code has nothing to with this question, i

If ptr->data is obtained from malloc, and you lose all pointers to it
here, you have a leak. free(ptr) does *not* free ptr->data too.

If you have a pointer to the same piece as in ptr->data somewhere else
too, and plan on freeing that later, fine - don't free it twice atleast.
jamesonang@gmail.com

2006-03-13, 7:51 am

many thanks.

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com