Re: strtok and strtok_r



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?

.



Relevant Pages

  • Re: strtok and strtok_r
    ... Other Posix utility functions are ... Your function should take a const char *. ... Why do you cast the result of realloc? ... Your function invokes undefined behaviour when running out of memory, ...
    (comp.lang.c)
  • Re: How to take in a string of any size?
    ... >the contents into the allocated memory. ... nobody said the string started at the beginning of the file. ... >number of calls to realloc() isn't going to be excessive). ...
    (comp.lang.c)
  • Re: CString
    ... Further, since CString resizes generally allocate an entire new block of memory before freeing the current one, the largest string is probably significantly less than that. ... I would use realloc() to grow the memory as this provides some chance that the block does not need to be moved. ...
    (microsoft.public.vc.mfc)
  • Re: Buffer or Realloc?
    ... better to allocate memory and realloc it for the size of the what is ... between deciding to use a fixed size buffer or allocating memory ... so for the string I've got to prepare as part of a message to the UK Government gateway where the specification says the string has a maximum length of 10 characters I should not use a fixed size buffer but a reallocating buffer? ... with the realloc() approach -- obviously ...
    (comp.lang.c)
  • Re: memory optimization
    ... > reallocate the array elements and add it an additional element. ... the net savings is three bytes per string. ... > appears that the whole memory consumed by the first method is less ... realloc() function works its magic? ...
    (comp.lang.c)