Usenet comp.unix.osf.osf1 (1992-2004)
 help / color / mirror / Atom feed
From: timw@isac.hces.com (Timothy M. Wieman)
Subject: Re: malloc() wierdness
Date: 1995/09/25	[thread overview]
Message-ID: <1995Sep25.154119.11872@isac.hces.com> (raw)
In-Reply-To: BIGFOOT.95Sep24160041@pyrus.astrakan.hgs.se

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 <Xsh> va=1400c5041 pc=3ff801297a8 ra=3ff80129334 type=ldq
: Unaligned access pid=11504 <Xsh> 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.



      reply	other threads:[~1995-09-25  4:00 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1995-09-24  4:00 malloc() wierdness Benny Holmgren
1995-09-25  4:00 ` Timothy M. Wieman [this message]
read-only archive

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox