From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fee84,57489bbe3b518aa X-Google-Attributes: gidfee84,public From: timw@isac.hces.com (Timothy M. Wieman) Subject: Re: malloc() wierdness Date: 1995/09/25 Message-ID: <1995Sep25.154119.11872@isac.hces.com>#1/1 X-Deja-AN: 110734945 references: organization: PACE Health Management Systems newsgroups: comp.unix.osf.osf1 Benny Holmgren (bigfoot@Astrakan.HGS.SE) wrote: : Hello. : I'm having trouble to allocate memory with malloc() under alpha/osf1 2.1. : I'm getting the following message to stderr. Can anyone explain to me what's : going on? : Regards, : Benny : --------------------------- Error message follows -------------------------- : Unaligned access pid=11504 va=1400c5041 pc=3ff801297a8 ra=3ff80129334 type=ldq : Unaligned access pid=11504 va=1400c5082 pc=3ff801297ac ra=3ff80129334 type=ldq : ..... : -- : Benny Holmgren bigfoot@astrakan.hgs.se : Astrakan Computer Club tel. +46-(0)26-183573 : Sweden "It's not about length, it's shoesize" You're trying to access memory that isn't aligned on a boundary. When memory is allocated (both automatically and explicitly) it is allocated on a boundary. Correct me if I'm wrong, but the alignment is as follows: char - 1 byte short - 2 byte int,float - 4 byte long,double,pointer - 8 byte For example, a structure defined as: struct teststru { char ch; long testlong; }; has wasted 7 bytes of space, because the long must be allocated on an 8 byte boundary (the memory address must be divisible by 8). The size of this struct is 16 bytes. The following code fragment will give an unaligned memory access error: char buf[20]; int *intptr; intptr = (int*) &buf[1]; printf("%d\n", *intptr); because the printf statement accesses an int which isn't on a 4 byte boundary. Changing the assignment of intptr to: intptr=(int*)&buf[4] will fix the unaligned access. Hope this helps. Tim -- ------------------------------------------------------------------------ Timothy M. Wieman | Software Engineer | timw@hces.com | PACE Health Management Systems, Inc.