R
Richard G. Riley
In another thread it was pointed out that I'd made a booboo with
strcpy : one that that I've, if I'm honest, made many times
before. Not out of badness, just because since I first programmed C
back in 1986 (and have done so for about 25 % of the time since then)
or so I never really looked at the manpage for strcpy :
this combined with K&Rs famous pointer lessons which lead to 2 or 3
versions of a linear start to finish strpy implementatin meant that on
some occasions I used strcpy to move blocks of memory which may
overlap in a character buffer. Bad. Sloppy. This combined with swapping
between languages probably made me a little careless.
Anyway, my question is this:
Why would any language comittee decide to make strcpy work in an
undefined manner for an overlapping object? It seems to me to be as
valid to do the same for memmove.
In the platform/compiler implementation for strcpy you can do
something very quick/CPU specific from start to finish (which doesnt
mind about overlap) or not as you please. If there is overlap and the
instructions being used would corrupt the operation then, and only
then, branch off to a more robust copy using a call to memmove or
something similar. The use of memmove invariably results in a possibly
unnecessary strlen so why not just wrap it all in the strcpy function?
Any comments appreciated
strcpy : one that that I've, if I'm honest, made many times
before. Not out of badness, just because since I first programmed C
back in 1986 (and have done so for about 25 % of the time since then)
or so I never really looked at the manpage for strcpy :
this combined with K&Rs famous pointer lessons which lead to 2 or 3
versions of a linear start to finish strpy implementatin meant that on
some occasions I used strcpy to move blocks of memory which may
overlap in a character buffer. Bad. Sloppy. This combined with swapping
between languages probably made me a little careless.
Anyway, my question is this:
Why would any language comittee decide to make strcpy work in an
undefined manner for an overlapping object? It seems to me to be as
valid to do the same for memmove.
In the platform/compiler implementation for strcpy you can do
something very quick/CPU specific from start to finish (which doesnt
mind about overlap) or not as you please. If there is overlap and the
instructions being used would corrupt the operation then, and only
then, branch off to a more robust copy using a call to memmove or
something similar. The use of memmove invariably results in a possibly
unnecessary strlen so why not just wrap it all in the strcpy function?
Any comments appreciated