Conor Daly wrote:
>> > if (my_buf)
> > {
> > char *tmp = realloc(my_buf, strlen(my_buf) +
> > strlen(next_string) + 1);
> > if(tmp == NULL)
> > {
> > fprintf(stderr, "realloc failed - not modifying my_buf\n");
> > }
> > else
> > {
> > tmp = my_buf;
> ^^^^^^^^^^^^^
> > strncat(my_buf, next_string, strlen(next_string)+1);
> > }
> > }
> > else /* The rest looked OK */
>> What's this line doing? AFAICT, "realloc(my_buf,<new-size>);" has already
> increased the amount of memory allocated to my_buf. tmp is a pointer to
> the same chunk of memory but is not required for operations on my_buf.
> from 'man realloc'
Yup - big typo - should have been
my_buf = tmp;
Also (as I've done) the strncat should be after my_buf = tmp; in the
same block, rather than outside it (as the code is currently).
> if( (realloc(my_buf, <size>)) == NULL ) {
> fprintf(stderr, "realloc failed - not modifying my_buf\n");
> } else {
> strncat(my_buf, next_string, strlen(next_string)+1);
> }
This is wrong... realloc doesn't necessarily extend the end of the
existing space in my_buf, it may well move it elsewhere. realloc returns
the address of the new memory block.
That'll teach me to test code I post to mailing lists :)
Cheers,
Dave.
--
David Neary, E-Mail dave.neary at palamon.ie
Palamon Technologies Ltd. Phone +353-1-634-5059
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!