From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 111c93,ce1ac29ce5ab112d X-Google-Attributes: gid111c93,public From: David Hagood Subject: Re: Dealing with unaligned accesses Date: 1998/12/09 Message-ID: <366E7769.4B6FF2D@sktc.net>#1/1 X-Deja-AN: 420419366 Content-Transfer-Encoding: 7bit References: <36673795.6C675A97@netcom.com> X-Accept-Language: en Content-Type: text/plain; charset=us-ascii Organization: SouthWind Internet Access, Inc. Mime-Version: 1.0 Newsgroups: comp.os.linux.alpha > access occurs. The data item at that address is an eight byte pointer. > The first byte of the structure is at address 2017fbd4. This means that > moving the pointer to the top of the structure won't help. In fact, the > structure is an even number of 64 bits in length. First, re-order the structure so that it allocates the biggest items first: pointers and arrays, then shorts, then chars. Second, modify the malloc call to look something like this: foo *my_pointer; void *save_ptr; save_ptr = malloc(sizeof(foo)+8); my_pointer = (foo *)(((unsigned long)save_ptr+7)&(~7)); This will force the actual pointer to be qword aligned no matter what malloc returns. There is one gotcha: you cannot free my_pointer, you must free save_ptr. Malloc ought to return aligned memory; perhaps there's a bug in libc/glibc.