free()
Mohsen Pahlevanzadeh
mohsen@pahlevanzadeh.org
Sun Sep 27 13:01:00 GMT 2009
Dear Axel & all,
I changed given func to the following func:
//////////////////////////////////////////////////////////////////
void safe_free(void * p){
memset(&p, 0, sizeof(p));
free(p);
p = 0;
}//end of safe_free func
///////////////////////////////////////////////////////////
Very very thank you.......
On Sun, 2009-09-27 at 14:13 +0200, Axel Freyn wrote:
> 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@pahlevanzadeh.org> 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
> > > >
> > >
> > >
> >
More information about the Gcc-help
mailing list