Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #21958

Re: copy size

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> (permalink)
Date Fri, 08 Jun 2012 10:30:02 -0400
From pete <pfiland@mindspring.com>
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> <lnehpqcqe3.fsf@nuthaus.mib.org> <o5u3t7t3os1rvojjgtihk67bmhnlt5eqc7@4ax.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

Show key headers only | View raw


David T. Ashley wrote:
> 
> On Thu, 07 Jun 2012 22:40:52 -0700, Keith Thompson <kst-u@mib.org>
> 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 <string.h> 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

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

copy size sinbad <sinbad.sinbad@gmail.com> - 2012-06-07 22:16 -0700
  Re: copy size Keith Thompson <kst-u@mib.org> - 2012-06-07 22:40 -0700
    Re: copy size David T. Ashley <dashley@gmail.com> - 2012-06-08 09:15 -0400
      Re: copy size Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-06-08 09:32 -0400
        Re: copy size David T. Ashley <dashley@gmail.com> - 2012-06-08 09:40 -0400
          Re: copy size Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-06-08 10:31 -0400
          Re: copy size Keith Thompson <kst-u@mib.org> - 2012-06-08 11:53 -0700
          Re: copy size Tim Rentsch <txr@alumni.caltech.edu> - 2012-06-08 12:11 -0700
            Re: copy size "christian.bau" <christian.bau@cbau.wanadoo.co.uk> - 2012-06-08 12:32 -0700
              Re: copy size Tim Rentsch <txr@alumni.caltech.edu> - 2012-06-08 20:42 -0700
      Re: copy size pete <pfiland@mindspring.com> - 2012-06-08 10:30 -0400
      Re: copy size Tim Rentsch <txr@alumni.caltech.edu> - 2012-06-08 11:47 -0700
    Re: copy size pete <pfiland@mindspring.com> - 2012-06-08 10:05 -0400

csiph-web