David T. Ashley <> writes:
> On Fri, 08 Jun 2012 09:32:34 -0400, Eric Sosman
> <> wrote:
>
>>> b)Allowing memcpy(dptr, sptr, 0) means that you decided to push logic
>>> to handle the size=0 case down into the function, by definition. It
>>> could just as easily be handled by the caller. In some programs, the
>>> case of size=0 and an invalid pointer go together. It seems useful to
>>> make it standard that if size=0, neither pointer will be dereferenced.
>>
>> This seems self-contradictory. If the caller can handle a
>>special case "just as easily," why is it "useful" to require the
>>internals of memcpy() to handle it in a particular way?
>
> Please don't play Devil's Advocate because you are bored : ).
>
> Creating a memcpy() function that doesn't handle the size=0 case is
> just as silly as creating a square root function that calculates
> square root for every non-negative integer except 0.
sqrt() computes square roots; sqrt(0.0) computes the square root of
0.0. It's not a special case.
memcpy() copies memory; memcpy() with size=0 doesn't copy any memory.
And as Eric points out, the standard already requires support for
memcpy() with size=0 if the s1 and s2 arguments are valid. If they're
not valid, the behavior is undefined, but I don't see that that's a
significant problem.
> It forces the users to either a)wrap the function or b)handle the
> exception case many times whereas handling it in the function would
> require handling it only once.
The only reason to wrap the function or handle exceptional cases is if
you have a call to memcpy() where it's possible to have size=0 *and* one
or more null pointer or otherwise invalid arguments. I don't see that
happening much in real life.
--
Keith Thompson (The_Other_Keith)
kst- <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"