------- Additional Comments From maarten at contemplated dot nl 2005-05-05 11:49 ------- (In reply to comment #2)
You are completely right, the code above merely demonstrates what happens when one writes to an illegal address. The correct version, *((int32_t *) &a.unaligned_int32) = 0x123456; does work. Sorry, result of 'compressing' the problem. My 'unaligned write' problem is, however, still there. I stumbled over it when trying to compile code where copying of 16 bytes (in a char array) was implemented as two reads and writes of int64_t. The problem occurs when typecasting char arrays to integer types, see modified example: 1: #include <stdint.h> int main(int argc, char *argv[]) { char x[100]; *((int32_t *) ((char *) (x))) = 0x123456; // ok *((int32_t *) ((char *) (x+4))) = 0x123456; // ok *((int32_t *) ((char *) (x+1))) = 0x123456; // fails return(0); } 2: (more related to original) #include <stdint.h> #pragma pack(1) typedef struct { int8_t byte; // removing this byte, code executes char unaligned_int32[4]; } test_unaligned; #pragma pack() int main(int argc, char *argv[]) { test_unaligned a; *((int32_t *) a.unaligned_int32) = 0x123456; // fails return(0); } -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=21387