----- Original Message -----
From: "Conor Daly" <conor.daly at oceanfree.net>
To: <ilug at linux.ie>
Sent: Thursday, July 05, 2001 9:14 AM
Subject: Re: [ILUG] C string concat question revisited
> On Wed, Jul 04, 2001 at 10:16:47AM +0100 or so it is rumoured
hereabouts,
> Dave Neary thought:
> > Conor Daly wrote:
> > >
> > > > if (my_buf)
> > > > {
> > > > char *tmp = realloc(my_buf, strlen(my_buf) +
> > > > strlen(next_string) + 1);
>> The only question left here is:
>> Does 'tmp = realloc(my_buf,<size>);' free the block pointed to by
my_buf
> while returning the address of the new block into tmp?
It would have to.
void *realloc(void *p, size_t size) is supposed to modify the size of
the object pointed to by p. If necessary, a new memory block would
have to be allocated and the old contents of *p copied in, but this
need not always be the case. For instance, if size was smaller that
the amount currently allocated for p, or if there was sufficient free
space after p's current memory allocation to allow that allocation
simply to be expanded. So you can have tmp==my_buf after the above
call, with no new memory block being allocated. So it's up to the
library to know whether a new block has been allocated, and therefore
whether the old block should be freed.
Not saying that this happens for any particular OS or library, but
realloc is a standard function and the standard does not guarantee
that a new memory block has been allocated.
FP
Maintained by the ILUG website team. The aim of Linux.ie is to
support and help commercial and private users of Linux in Ireland. You can
display ILUG news in your own webpages, read backend
information to find out how. Networking services kindly provided by HEAnet, server kindly donated by
Dell. Linux is a trademark of Linus Torvalds,
used with permission. No penguins were harmed in the production or maintenance
of this highly praised website. Looking for the
Indian Linux Users' Group? Try here. If you've read all this and aren't a lawyer: you should be!