Re: free()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Dear Mohsen,

you are speaking about different things;-)
Steffen proposed, to zero out the Memory - in order to guarantee, that
nobody will be able to access the data where "p" pointed to
That could be done by
memset(p, 0, sizeof(struct linked_list));
what you did (p=0;) is CHANGING the Pointer: you set the address, where
p points to, to zero - and AFTERWARDS you free this address: you try to
free the address "0". The result of your code is a memory leak: the
memory you allocated for p before will never be freed.
The "safest" way would be:

memset(p, 0, sizeof(struct linked_liste));
free(p);
p=0;

so: 
 - first, set the memory where p points to, to zero
 - second, free this memory
 - third, set the pointer to zero.

HTH,

Axel

On Sun, Sep 27, 2009 at 03:32:01AM +0330, Mohsen Pahlevanzadeh wrote:
> Dear Steffen & all,
> According to your description, i wrote following func & it work well:
> ///////////////////////////////////////////////////////
> void safe_free(void * p){
>         p = 0;
>         free(p);
> }//end of safe_free func
> ///////////////////////////////////////////////////////
> Thank you.
> 
> On Sun, 2009-09-27 at 11:37 +0200, Steffen Wendzel wrote:
> > You have to zero the memory it before, like I described here:
> > 
> > http://www.wendzel.de/dr.org/libcmle/examples/mem.html
> > 
> > Steffen
> > 
> > On Sun, 27 Sep 2009 00:18:00 +0330
> > Mohsen Pahlevanzadeh <mohsen@xxxxxxxxxxxxxxxxx> wrote:
> > 
> > > Dear all,
> > > We are working on C code (not ++),So we must use free instead delete.
> > > I have following code:
> > > /////////////////////////////////
> > >         struct linked_list *p;
> > > 	p->src="10.0.0.1";
> > >         free(p);
> > >         printf ("%s",p->srcip);
> > > /////////I see in my output 10.0.0.1 
> > > My question: i drop p pinter, but see it's value, how i kill p with its
> > > value?
> > > 
> > > Yours,
> > > Mohsen
> > > 
> > 
> > 
> 

[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux