strdup()

M

Mark McIntyre

Peter Shaggy Haywood wrote:
Oh Peter, you're such an old woman. Surely defining strdup() will get
you in trouble only if it already exists, in which case you don't need
to define it yourself. :)

.... except that its forbidden by the language specification, so your
compiler is entitled to get sniffy with you...
 
P

Peter Shaggy Haywood

Groovy hepcat Chris Croughton was jivin' on Tue, 22 Mar 2005 11:55:03
+0000 in comp.lang.c.
Re: strdup()'s a cool scene! Dig it!
Correct. It's trivial to implement, though:

Yes, if you want to have nasal demons for tea. Identifiers with
external linkage beginning with str and a lower case letter are
reserved.

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
 
C

Chris Croughton

Groovy hepcat Chris Croughton was jivin' on Tue, 22 Mar 2005 11:55:03
+0000 in comp.lang.c.
Re: strdup()'s a cool scene! Dig it!


Yes, if you want to have nasal demons for tea. Identifiers with
external linkage beginning with str and a lower case letter are
reserved.

If it's already defined, you don't need to define it! On any POSIX
compliant system it will be defined anyway, so all you need to do when
porting is to determine whether the new system has it and if not enable
your own version. My applications do that automatically when
configuring for a new installation, because in practice real world
applications need access to many non-standard things (the C spec.
doesn't know anything about file systems and directories, for example,
but real users need that access), strdup is just one of many things
which might be there and if not need to be replaced.

In practice, any new functions in a future standard will be compatible
(or at least not clash with) with other existing standards like POSIX,
because if they aren't they will cause breakage in too much code and
the new standard just won't be used and will fall into disrepute.

Chris C
 
C

Chris Croughton

Groovy hepcat Chris Croughton was jivin' on Sun, 27 Mar 2005 13:01:22
+0100 in comp.lang.c.
Re: strdup()'s a cool scene! Dig it!


So you want to write a different version of the code for each
implementation you compile it on? Well, if that's what floats your
boat then who am I to stifle you? But I prefer to write code that's
portable.

If you want to limit yourself to those few programs which are totally
portable (nothing which involves directory access, for a start), I'm
not stopping you. I need to write programs in the real world, however,
where it can't be guaranteed that even all of the C89 functions will be
implemented, so I need to provide replacements for them.

Since it's in my library of "things I find useful", the only 'porting' I
need to do is to determine whether it is available on the target
platform, and for most of them that will happen automatically. That
sort of thing needs to be done anyway to determine headers for doing
filesystem access (read directories), sockets, pipes, whether long long
exists, and many more non portable but essential features in real
programs.
And what about all those implementations that are not POSIX
compliant but have extentions named strdup which may or may not follow
the usual idioms? You'd have to make special arrangements for them.

What implementation has strdup() which does not follow the idiom? I
haven't found one in the last 20+ years, and I very much doubt that
anyone would be foolish enough to write one now.
With a function as trivial as the one we're discussing here, it is so
much easier to just write it yourself portably than to do what you
suggest.

Duh, I did just write it portably.
True. But why make the situation worse by invoking undefined
behaviour?

It is only 'undefined' by the C standard, which is not the only standard
in real world programs.
And what about all those implementations that don't follow existing
standards like POSIX? They often do things different just to be
difficult. You should take that into account when writing code.

Examples? As I said, I have never found any library implementations of
strdup() which do anything other than what the POSIX standard says (even
before POSIX existed). The worst is on K&R systems which didn't support
const, but that will blow most C89 and later programs anyway.
Oh, is that what'll happen, eh?

Anything which breaks POSIX will just be ignored by the vast majority of
the programmers, because there is too much POSIX compliant code which
would be affected. Having standards which are ignored causes them to
fall into disrepute (this is happening anyway with C99, very few places
are at all interested in going to C99 compliant compilers), just as it
does with laws.

I just did a scan of some of the Free Software source on my system.
Every project there uses strdup(). Perl, Python, Qt, openSSL, fvwm,
gcc, procmail, GPG, Doxygen, all of the libraries, etc. (it's almost 100
directories, I'm not going to bother to list them all). I somehow think
that anyone re-using it as something different would not be popular...

Chris C
 
P

Peter Shaggy Haywood

Groovy hepcat Chris Croughton was jivin' on Sun, 27 Mar 2005 13:01:22
+0100 in comp.lang.c.
Re: strdup()'s a cool scene! Dig it!
If it's already defined, you don't need to define it! On any POSIX

So you want to write a different version of the code for each
implementation you compile it on? Well, if that's what floats your
boat then who am I to stifle you? But I prefer to write code that's
portable.
compliant system it will be defined anyway, so all you need to do when
porting is to determine whether the new system has it and if not enable

And what about all those implementations that are not POSIX
compliant but have extentions named strdup which may or may not follow
the usual idioms? You'd have to make special arrangements for them.
With a function as trivial as the one we're discussing here, it is so
much easier to just write it yourself portably than to do what you
suggest.
your own version. My applications do that automatically when
configuring for a new installation, because in practice real world
applications need access to many non-standard things (the C spec.

True. But why make the situation worse by invoking undefined
behaviour?
doesn't know anything about file systems and directories, for example,
but real users need that access), strdup is just one of many things
which might be there and if not need to be replaced.

In practice, any new functions in a future standard will be compatible
(or at least not clash with) with other existing standards like POSIX,

And what about all those implementations that don't follow existing
standards like POSIX? They often do things different just to be
difficult. You should take that into account when writing code.
because if they aren't they will cause breakage in too much code and
the new standard just won't be used and will fall into disrepute.

Oh, is that what'll happen, eh?

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
 
A

Alan Balmer

If you want to limit yourself to those few programs which are totally
portable (nothing which involves directory access, for a start), I'm
not stopping you. I need to write programs in the real world, however,
where it can't be guaranteed that even all of the C89 functions will be
implemented, so I need to provide replacements for them.

I'm a little puzzled. Would your function stop working if you named it
"dupstr" instead of "strdup"?

You make a good (if obvious) case for writing functions of your own,
but no case for violating the implementation name space.
 
T

Tor Rustad

Richard Bos said:
Whoever wrote this rule does not understand the perversity of human
nature. What's to stop me from declaring a humungous static array, and
getting my dynamic memory from it?

Whoever try to fool automated code checkers this way, will for sure
piss off the person doing the external code review! :)

Well-checked dynamic allocation is
perfectly safe; this rule should read more like "All dynamic memory
allocation shall be checked, and appropriate measures taken on
failure".

Nope, *non-deterministic* memory usage, should of course
be avoided in safety-critical systems.

However, there might be cases where dynamic memory based
algorithms are far simpler to implement, i.e. a valid point then,
is that "the cure might be worse than the disease".
That's not an argument for dying in a malloc() wrapper, that's an
argument for more error recovery elsewhere.

What kind of malloc error recovery do you talk about?

If the system is out of resources, then there isn't that simple
to recover from it. Ignoring HW faults, either your program has
a bug _or_ your system need more memory to continue to run
the program (assuming no GC is in use).

I have written fault-tolerant servers that can do recovery from
malloc failures, but in many cases such fault-tolerant
programming is simply not needed.
 
M

Mark McIntyre

*non-deterministic* memory usage, should of course
be avoided in safety-critical systems.

non-deterministic? You mean you find yourself often allocating quantum
amounts of memory, or random amounts, or imaginary amounts, or amounts
at relativistic velocities? Wow....
 
K

Keith Thompson

Mark McIntyre said:
non-deterministic? You mean you find yourself often allocating quantum
amounts of memory, or random amounts, or imaginary amounts, or amounts
at relativistic velocities? Wow....

Non-deterministic, in this context, probably means that the amount of
memory allocated may depend on input to the program.

A program that allocates exactly 42 fixed-size buffers during startup
has deterministic memory usage. One that allocates a fixed-size (or
variable-size) buffer each time some external event occurs has
non-deterministic memory usage.

In a text editor or a game it's acceptable to run some finite risk of
running out of memory (and possibly crashing) under unusual
circumstances. In a safety-critical system, it's often important to
prove rigorously that this can *never* happen (assuming reliable
hardware).
 
R

Richard Bos

Tor Rustad said:
Whoever try to fool automated code checkers this way, will for sure
piss off the person doing the external code review! :)

I trusty the person doing the review better than a hard and fast rule.

Richard
 
P

pete

Chris said:
On Mon, 28 Mar 2005 18:05:44 GMT, Peter "Shaggy" Haywood


Examples?

To me, strdup is just another homework exercise out of K&R, like itoa.

Is POSIX strdup the same as K&R's?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top