From mboxrd@z Thu Jan 1 00:00:00 1970 From: albaugh@agames.com (Mike Albaugh) To: help-gcc@gnu.org Subject: Re: ALIGNMENT / going crazy with gcc-2.7.2-sparc-sun-sunos-4.1.3 Date: Fri, 31 Dec 1999 22:24:00 -0000 Message-ID: <83ehv8$oc9$1@null.agames.com> References: <83earg$bgg$1@nslave1.tin.it> X-SW-Source: 1999-12n/msg00261.html Message-ID: <19991231222400.TQSSTYdm8TmFEJsx08PP_6A1rOVN_M07LYVD9dGdJGg@z> cris (chitty@libero.it) wrote: : hi! : how can i tell gcc to compile my programs using strict-alignment for data : types? It seems rather that you are trying to do the opposite. : if i write smth like this: : void *full_array = malloc(50); : short *short_p = &(( (char*) full_array)[ N ]); : printf("%hd\n",*short_p); : if N is odd, i get a core dump! Well, yes. You _asked_ it to get a short from an odd address malloc() always returns "maximum possible needed alignment" memory, so the address will tend to have several zeros in the LSBs. Add an od number to that and you get an odd number, so why would you _not_ expect a problem on any machine with any alignment issues? : i'm using gcc 2.7.2 on an old sparc2 with SunOs 4.1.3, no way to upgrade the : os or to get a newer build of gcc. : i've searched in the doc., i've found -m switches regarding alignment for : ibm machines, but none for sparc ones! : i've also seen a SHORT_ALIGNMENT macro to be defined (gcc-info, "You can : control compilation driver"), but i don't know WHERE to define it! Simply put, there is no way to get an "already compiled" library routine to "know" that it has to pick up every larger-than-one-byte data item "with tweezers". Even if there was, would you really want to use such a system? Think about it, _every_ memory reference done a byte at a time unless the compiler can "prove" that it can't fault. This is the "nasty little secret" about __attribute__ aligned (or prgama pack). The compiler "knows" to "be careful" in the piece of code that actually contains the structure definition, but if you take the address of mis-aligned short within a "packed" structure, and pass it to a function that expects a legitimate "pointer to short", "bad things could happen" (Don't cross the beams!) Mike | albaugh@agames.com, speaking only for myself, and soon to abandon Usenet, | at least for a while, Happy "end of civilization as we know it" :-)