Re: strtok and strtok_r
- From: Francine.Neary@xxxxxxxxxxxxxx
- Date: Sat, 15 Sep 2007 06:07:50 -0700
On Sep 15, 2:02 pm, "Charlie Gordon" <n...@xxxxxxxxxxx> wrote:
<Francine.Ne...@xxxxxxxxxxxxxx> a écrit dans le message de news:
1189858659.251030.194...@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
On Sep 15, 3:31 am, "Charlie Gordon" <n...@xxxxxxxxxxx> wrote:
"Sam Harris" <nos...@xxxxxxxx> a écrit dans le message de news:
slrnfemebs.ug9.spam...@xxxxxxxxxxxxxxxxx
On 15 Sep 2007 at 1:28, Charlie Gordon wrote:
Why did C99 get published without including the reentrant alternatives
to
strtok and similar functions is a mystery. I guess the national bodies
were
too busy arguing about iso646.h. Other Posix utility functions are
missing
for no reason: strdup for instance. Did the Posix guys patent those or
is
WG14 allergic to unix ?
You can easily write your own version of strdup in a couple lines. I use
the following:
char *strdup(char *s)
{
char *r=0;
int i=0;
do {
r=(char *) realloc(r,++i * sizeof(char));
} while(r[i-1]=s[i-1]);
return r;
}
This proves my point.
Adding useful functions like strdup would prevent newbies and jokers from
re-inventing them in the most cumbersome, inefficient, ugly error prone
ways.
Your function should take a const char *.
sizeof(char) is 1 by definition
Why do you cast the result of realloc ?
Your function invokes undefined behaviour when running out of memory, it
should return NULL instead.
Even if it doesn't run out of memory, there's no reason to assume
realloc won't return a pointer to a different area of memory each
time: with the code above, this will lead to 1) memory leaks; 2) the
first part of the string is not copied properly.
No assumption is made about the return value of realloc pointing to
the same area. The above code will indeed cause memory leak when
running out of memory, but undefined behaviour will have been invoked
already since NULL is dereferenced then. Apart from that, the string
is copied correctly because realloc does preserve the contents of the
block it reallocates upto the smaller of old and new sizes.
The name strdup is also reserved for the implementation.
That's one more reason it should have been standardized in C99.
My suggestion would be:
#include <stdlib.h>
#include <string.h>
char *my_strdup(const char *s)
{
size_t len;
char *t;
if(t=malloc(len=strlen(s)+1))
memcpy(t, s, len);
return t;
}
You code performs the task, but I find it misleading to call len a var
iable that is not the length of the string. I prefer to use size for
this purpose.
Furthermore, this code would not pass my default warning settings.
Assignment as an test expression is considered sloppy and error prone.
Tell that to Kernighan and Ritchie. :)
--
Chqrlie.
Is there a reason for the typo in your signature?
.
- Follow-Ups:
- Re: strtok and strtok_r
- From: Charlie Gordon
- Re: strtok and strtok_r
- References:
- strtok and strtok_r
- From: siddhu
- Re: strtok and strtok_r
- From: CBFalconer
- Re: strtok and strtok_r
- From: Charlie Gordon
- Re: strtok and strtok_r
- From: Martien verbruggen
- Re: strtok and strtok_r
- From: Charlie Gordon
- Re: strtok and strtok_r
- From: Sam Harris
- Re: strtok and strtok_r
- From: Charlie Gordon
- Re: strtok and strtok_r
- From: Francine . Neary
- Re: strtok and strtok_r
- From: Charlie Gordon
- strtok and strtok_r
- Prev by Date: Re: strtok and strtok_r
- Next by Date: Re: file of exact size
- Previous by thread: Re: strtok and strtok_r
- Next by thread: Re: strtok and strtok_r
- Index(es):
Relevant Pages
|