|
From: Ioannis V. <no...@ho...> - 2002-09-03 18:31:22
|
> -----Original Message-----
> From: Ioannis Vranos [mailto:no...@ho...]=20
> Sent: Tuesday, September 03, 2002 9:27 PM
> To: 'dev-cpp-users-list'
> Subject: RE: [Dev-C++] realloc
>=20
>=20
> > -----Original Message-----
> > From: dev...@li...=20
> > [mailto:dev...@li...] On Behalf=20
> > Of Abhijit Shylanath
> > Sent: Tuesday, September 03, 2002 7:58 PM
> > To: Carlos Garc=EDa del Monte; dev-cpp-users-list
> > Subject: Re: [Dev-C++] realloc
> >=20
> > Then maybe this?
> >=20
> > char *ptr, *tmp_ptr;
> > int buf_size =3D BUF_SIZE;
> >=20
> > *ptr =3D (char *)malloc(BUF_SIZE); // Assuming success
> > *tmp_ptr =3D realloc(ptr, NEW_BUF_SIZE);
> > if(tmp_ptr)
> > {
> > free(ptr);
>=20
>=20
> This is an error. If (tmp_ptr) means if realloc() succedeed.=20
> In this case ptr has already been freed.
For accuracy this is an error because of the following.
If realloc() succeeded:
a) It resized at the same place, then ptr is returned.
b) If local resizing was not feasible, it allocated a separate space,
copied the corresponding data and erased ptr.
In case a you erase the whole thing (tmp_ptr).
In case b you erased already freed space.
So in summary if the operation succeeds do not care for the old address.
Ioannis
* Ioannis Vranos
* Programming pages: http://www.noicys.cjb.net
* Alternative URL: http://run.to/noicys
|