memcpy

Bruce Blodgett blodgett at apollo.HP.COM
Fri Sep 21 08:21:00 AEST 1990


In article <1990Sep19.021418.11574 at maths.tcd.ie>, tim at maths.tcd.ie
(Timothy Murphy) wrote:
> In unzip.c it is assumed that the effect of
> 	buf[0] = c;
> 	memcpy(buf+1, buf, 20);
> is to set
> 	buf[0] = buf[1] = buf[2] = ... = buf[21] = c.
...
> Is this a bug on the Sparc,
> or is memcpy not fully specified?

According to the ANS C standard, section 4.11.2.1, "If copying takes
place between objects that overlap, the behavior is undefined."  Thus
whatever a C compiler wants to do is "correct" in this case.

By the way, why don't you use the much safer standard function:
    memset(buf, c, 21);
which sets buf[0] = buf[1] = buf[2] = ... = buf[20] = c.
If you want buf[21] == c (which is NOT what your original code does),
make the 3rd argument 22 rather than 21.
Bruce Blodgett
blodgett at apollo.hp.com
(508) 256-0176 x4037



More information about the Comp.std.c mailing list