Explanation, please!

der Mouse mouse at mcgill-vision.UUCP
Fri Sep 2 16:58:42 AEST 1988


In article <9064 at pur-ee.UUCP>, hankd at pur-ee.UUCP (Hank Dietz) writes:
> In article <189 at bales.UUCP>, nat at bales.UUCP (Nathaniel Stitt) writes:
>> ["Portable Optimized Copy" routine]
> As long as we're getting into structured, portable, hacks, let me
> suggest the following two ways of doing block copy:

> 1.    If the number of items/bytes is known at compile time, then you
>       can define a struct type of the appropriate size and use struct
>       assignment with type casts to make it fly.  For example,

[ int *p; int *q; /* want to copy 601 ints from p to q */ ]
>       struct t601 { int t[601]; };
>       *((struct t601 *) q) = *((struct t601 *) p);

Surprise!  Your compiler has sizeof(int)=2, sizeof(long)=4, and aligns
structs on long boundaries.  You just copied 602 ints!

>       Unfortunately, the above code should have been written as:

>       if (n & 512) {
>               *(((struct t512 *) q)++) = *(((struct t512 *) p)++);
>       }

>       but, for some unknown reason, the VAX C compiler didn't like
>       that.

Perhaps the "unknown reason" is that it's illegal!  An expression
formed by casting another expression is not an lvalue and never has
been.  Any compiler that accepts that is broken.  (Yes, there exist
such broken compilers.)

What would you expect from

    double d;

    ((int)d) ++;

Perhaps you'd want d+=1?  Perhaps d=1+(int)d?  Perhaps
*(int*)&d=1+(int)d even?

					der Mouse

			old: mcgill-vision!mouse
			new: mouse at larry.mcrcim.mcgill.edu



More information about the Comp.lang.c mailing list