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


Groups > comp.lang.c > #21958

Re: copy size

Message-ID <4FD20C6A.367B@mindspring.com> (permalink)
Date 2012-06-08 10:30 -0400
From pete <pfiland@mindspring.com>
Organization PF
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>

Show all headers | 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