Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > xstrcat function

Reply
Thread Tools

xstrcat function

 
 
=?ISO-8859-1?Q?Leonardo_Javier_Bel=E9n?=
Guest
Posts: n/a
 
      07-06-2003
I've been studing the way to have a more useful strcat function and I
realized that I need more than one argument to handle at a time, and
also a way to resize the pointer automatically so I can forget the
memory handling problem.

So I constructed a new xstrcat function but, although works pretty
well in most environments, I discover that it works erratically when I
try to add the result var on the list of strings to add (i mean if I
do a xstrcat(&tmp, tmp, NULL)), so I send this to the community to see
if someone with more experience than me can help.
Cheers,
Leo

char *
xstrcat (char **dest, char *str, ...)
{
va_list va;
size_t length = 0;
char *ptr, *tmp;

if (!str)
return (char *) NULL;

length += strlen (str);

va_start (va, str);
while ((tmp = va_arg (va, char *)))
length += strlen (tmp);
va_end (va);

if (!*dest)
*dest = malloc (length + 1);
else
*dest = realloc (*dest, (length + 1));

if (!dest)
return (char *) NULL;

ptr = *dest;

for (tmp = str; *tmp; tmp++)
*ptr++ = *tmp;

va_start (va, str);
while ((tmp = va_arg (va, char *)))
{
while (*tmp)
*ptr++ = *tmp++;
}
va_end (va);
*ptr = '\0';
return *dest;
}
 
Reply With Quote
 
 
 
 
Ben Pfaff
Guest
Posts: n/a
 
      07-07-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) (Leonardo Javier BelÚn) writes:

> So I constructed a new xstrcat function but, although works pretty
> well in most environments, I discover that it works erratically when I
> try to add the result var on the list of strings to add (i mean if I
> do a xstrcat(&tmp, tmp, NULL)), so I send this to the community to see
> if someone with more experience than me can help.


> char *
> xstrcat (char **dest, char *str, ...)
> {
> va_list va;
> size_t length = 0;
> char *ptr, *tmp;
>
> if (!str)
> return (char *) NULL;


The cast is gratuitous.

> length += strlen (str);
>
> va_start (va, str);
> while ((tmp = va_arg (va, char *)))
> length += strlen (tmp);
> va_end (va);
>
> if (!*dest)
> *dest = malloc (length + 1);
> else
> *dest = realloc (*dest, (length + 1));


1. realloc() acts like malloc() when the first argument is a null
pointer, so there's no need to switch to malloc() for that
case.

2. If realloc() fails, you probably just leaked memory, because
you overwrote the original pointer.

> if (!dest)


This does not test what you think it tests. If allocation
failed, *dest will be a null pointer, not dest.

> return (char *) NULL;


Gratuitous cast.

> ptr = *dest;
>
> for (tmp = str; *tmp; tmp++)
> *ptr++ = *tmp;
> va_start (va, str);
> while ((tmp = va_arg (va, char *)))
> {
> while (*tmp)
> *ptr++ = *tmp++;
> }
> va_end (va);
> *ptr = '\0';


The rest of this looks okay, though I'd prefer strcpy() followed
by strchr() over explicit loops.

> return *dest;
> }


--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Function versus pointer to function, in context of std::function,huh? Alf P. Steinbach C++ 10 07-27-2011 05:51 AM
Function pointer to void function and int function Giannis Papadopoulos C Programming 5 09-05-2005 09:06 PM
How override ALL function calls? (Is there a "function call function"?) seberino@spawar.navy.mil Python 2 08-01-2005 12:38 PM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM
Passing a C++ object's member function to a C function expecing a function pointer! James Vanns C++ 7 01-21-2004 02:39 AM



Advertisments