Xref: utzoo comp.lang.c:12325 comp.arch:6231 Path: utzoo!utgpu!water!watmath!clyde!att!osu-cis!tut.cis.ohio-state.edu!bloom-beacon!mcgill-vision!mouse From: mouse@mcgill-vision.UUCP (der Mouse) Newsgroups: comp.lang.c,comp.arch Subject: Re: Explanation, please! Message-ID: <1286@mcgill-vision.UUCP> Date: 2 Sep 88 06:58:42 GMT References: <638@paris.ics.uci.edu> <9064@pur-ee.UUCP> Organization: McGill University, Montreal Lines: 44 In article <9064@pur-ee.UUCP>, hankd@pur-ee.UUCP (Hank Dietz) writes: > In article <189@bales.UUCP>, nat@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@larry.mcrcim.mcgill.edu