T
Tinkertim
Hello,
I've written a function similar to strdup(), except that realloc() is
used and the # of characters written is returned instead of returning
a cast value of memcpy().
I made this to use in nested loops in lieu of strdup(), to avoid
freeing temporary vars until the end.
Use would be like this:
char *tmp;
.... loop ...
if (-1 == (redup(&tmp, string)))
(bail and complain)
.... end loop ...
if (tmp)
free(tmp);
Beyond failing to set errno, can anyone find anything wrong with this:
int redup(char **s1, const char *s2)
{
size_t len;
if (NULL == s2)
return -1;
len = strlen(s2) + 1;
if (NULL == (*s1 = realloc(*s1, len)))
return -1;
memset(*s1, 0, sizeof(*s1));
memcpy(*s1, s2, len);
return (int) len;
}
Thanks in advance! I'm putting together a small snippet collection for
trimming, tokenizing and managing strings .. I hope to enlist a few
other sets of eyes before I release it into the wild.
Cheers,
--Tim
I've written a function similar to strdup(), except that realloc() is
used and the # of characters written is returned instead of returning
a cast value of memcpy().
I made this to use in nested loops in lieu of strdup(), to avoid
freeing temporary vars until the end.
Use would be like this:
char *tmp;
.... loop ...
if (-1 == (redup(&tmp, string)))
(bail and complain)
.... end loop ...
if (tmp)
free(tmp);
Beyond failing to set errno, can anyone find anything wrong with this:
int redup(char **s1, const char *s2)
{
size_t len;
if (NULL == s2)
return -1;
len = strlen(s2) + 1;
if (NULL == (*s1 = realloc(*s1, len)))
return -1;
memset(*s1, 0, sizeof(*s1));
memcpy(*s1, s2, len);
return (int) len;
}
Thanks in advance! I'm putting together a small snippet collection for
trimming, tokenizing and managing strings .. I hope to enlist a few
other sets of eyes before I release it into the wild.
Cheers,
--Tim