Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!border3.nntp.dca.giganews.com!Xl.tags.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local2.nntp.dca.giganews.com!nntp.earthlink.com!news.earthlink.com.POSTED!not-for-mail NNTP-Posting-Date: Fri, 08 Jun 2012 09:30:02 -0500 Message-ID: <4FD20C6A.367B@mindspring.com> Date: Fri, 08 Jun 2012 10:30:02 -0400 From: pete Reply-To: pfiland@mindspring.com Organization: PF X-Mailer: Mozilla 3.04Gold (WinNT; I) MIME-Version: 1.0 Newsgroups: comp.lang.c Subject: Re: copy size References: <03c3ed4d-f3ce-437b-9476-1c512c7772de@o3g2000pby.googlegroups.com> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Lines: 58 X-Usenet-Provider: http://www.giganews.com NNTP-Posting-Host: 4.156.228.2 X-Trace: sv3-H5GRebgNalgZYFelut+DkFwKknpMWBnLubEJoAvaSac1zL5yPfyazFwFQbtKFWkjEXq4q7W5tUiLovY!cNPo2CtwaK/72o3Pie7KaIoW+rA/1yXlF75+7BZHzD793585psr80jq7BOglt1GLlVVmlMWbczu/!4nD2tbF+ X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 2666 Xref: csiph.com comp.lang.c:21958 David T. Ashley wrote: > > On Thu, 07 Jun 2012 22:40:52 -0700, Keith Thompson > wrote: > > > > >So yes, it's ok to call memcpy() with a size of 0. > > > >Note that if either s1 or s2 is a null pointer, the behavior is (I > >believe) undefined. > > I quick search of the Internet has revealed that you are right. > > But it seems odd for three reasons: > > a)In almost all the functions I write that have parameters of type > (void *) and size_t (usually occurring together, for the obvious > reasons), I handle the size=0 case. When I handle it, the most > natural construction of the function is that the pointer(s) are not > dereferenced if size=0. memcpy is of the functions. and there are some of them, where it may make sense to derefence a pointer parameter prior to evaluating the n parameter. char *strncat(char *s1, const char *s2, size_t n) { char *const p1 = s1; s1 += str_len(s1); while (n-- != 0 && *s2 != '\0') { *s1++ = *s2++; } *s1 = '\0'; return p1; } > > 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. memcpy may or may not, (depending on how it is implemented), require any special code to handle the n equals 0 case. void *memcpy(void *s1, const void *s2, size_t n) { unsigned char *p1 = s1; const unsigned char *p2 = s2; while (n-- != 0) { *p1++ = *p2++; } return s1; } -- pete