PAGE2GO2 HOME | INTERNET NEWS

LeighExchange - Free Advertising Network Stock Research at Internet Speed

Re: strdup

 List
Subject: Re: strdup
Poster: KeithThompsonkst-u@mib.org
Date: Fri, 23 Mar 2007 13:46:48 -0700
Related Postings: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
CBFalconer writes:
> Keith Thompson wrote:
>> CBFalconer writes:
>>> Richard Bos wrote:
>>>> Christopher Benson-Manica wrote:
>>>>> Richard Bos wrote:
>>>>>
>>>>>> Also _because_ it is so easy to write yourself.
>>>>>
>>>>> Too bad you're not allowed to call it "strdup" if you #include
>>>>> :-)
>>>>
>>>> *Shrug* Call it str_dup() and #define strdup str_dup, then.
>>>> That's legal.
>>>
>>> No it isn't.
>>
>> Yes, it is. C99 7.26.11, under "Future library directions":
>>
>> Function names that begin with str, mem, or wcs and a lowercase
>> letter may be added to the declarations in the header.
>
> No it isn't. See:
>
> 7.1.3 Reserved identifiers
>
> [#1] Each header declares or defines all identifiers listed
> in its associated subclause, and optionally declares or
> defines identifiers listed in its associated future library
> directions subclause and identifiers which are always
> reserved either for any use or for use as file scope
> identifiers.
>
> ... snip ...
>
> -- All identifiers with external linkage in any of the
> following subclauses (including the future library
> directions) are always reserved for use as identifiers
> with external linkage.143)

Is the str_dup() function or the strdup macro that you're saying is illegal?

str_dup is not a reserved identifier; it doesn't begin with str and a lowercase letter.

strdup, if defined as a macro, doesn't have external linkage (though you might need a "#undef strdup" if you've included ).

In particular, I claim that the following program:

#include #include #include

char *str_dup(const char *s) { char *result = malloc(strlen(s) + 1); if (result != NULL) { strcpy(result, s); } return result; }

#undef strdup #define strdup(s) str_dup(s)

int main(void) { char *s = strdup("hello, world"); if (s == NULL) { fputs("strdup() failed\n", stderr); return EXIT_FAILURE; } else { puts(s); return EXIT_SUCCESS; } }

is valid, and that it fails to be strictly conforming only because of the possibility of failure in the calls to malloc and perhaps puts. (There's some controversy about whether I/O failures break strict conformance; I don't want to get into that in this this thread.)

As a matter of style and robust coding, I prefer not to worry about the details of which identifiers are reserved for which purposes in which circumstances. It's easier just to avoid all potentially reserved identifiers in all circumstances than to keep track of all the rules. Because of that, I wouldn't write the above program in that way; I'd probably just name the function str_dup or dupstr and not worry about the macro. (Or I might use strdup if I didn't care about portability to non-POSIX systems.)

-- Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst> San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst> "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"

 

Page2Go2.com is not responsible for content of this message.