Article View: comp.std.c++
Article #1859Re: Alignment requirements
From: Steve Clamage
Date: Mon, 28 Aug 1995 23:10
Date: Mon, 28 Aug 1995 23:10
52 lines
2843 bytes
2843 bytes
In article 14306@nlm.nih.gov, bkline@cortex.nlm.nih.gov (Bob Kline) writes:
>
>I have found (on more than one occasion) published code which
>appears to rely on an assumption about c++ for which I haven't
>been able to find any justification in the ARM or the draft
>standard itself. The assumption is that a static array of
>char is guaranteed to be suitably aligned to hold objects of
>any types overlaid on the array's space. Here is an example
>from James Coplien's "Advanced C++ Programming Styles and
>Idioms" (page 149, ff.):
>
> unsigned char bigBuf[4096], *bigBufPtr = bigBuf;
>
> class LAPD {
> public:
> void *operator new(size_t) { return (void *)bigBufPtr; }
> ...
> };
>
>The details of the class are elided here, but several pages later
>the data members are shown to include a couple of pointers, which
>(at least on some machines with which I am familiar) would cause
>things to blow up if they were not placed on a suitably aligned
>memory boundary.
>
>So my question is: does the draft standard require that all arrays
>of char be placed by the compiler at an address which would meet
>the alignment requirements for any object which might be placed
>there? If so, where should I look in the draft to find this
>requirement?
An array of char must be suitably aligned for access to the char elements.
I don't believe there is any requirement that it be suitably aligned for
any other type. For this kind of general usage, I think you have to use an
array element type corresponding to worst-case alignment requirements.
For example, if long double has the severest alignment requirements (as on
the Sparc), you should write for example
long double bigBuf[4096/sizeof(long double)];
void* bigBufPtr = bigBuf;
It is a requirement that the space returned by operator new be suitably
aligned for any object of the requested size. The sample code probably works
on many systems, but I don't think the standard allows you to assume so.
---
Steve Clamage, stephen.clamage@eng.sun.com
[ comp.std.c++ is moderated. Submission address: std-c++@ncar.ucar.edu.
Contact address: std-c++-request@ncar.ucar.edu. The moderation policy
is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Technical Details
Message-ID:
<41t0sd$3ui@engnews2.Eng.Sun.COM>Path:
arc1-ca.pugleaf.net!engnews2.Eng.Sun.COM!taumet!clamageReferences:
<1995Aug28.164444.14306@nlm.nih.gov>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!engnews2.Eng.Sun.COM!taumet!clamage From: clamage@Eng (Steve Clamage) Newsgroups: comp.std.c++ Subject: Re: Alignment requirements Date: 28 Aug 1995 23:10:37 GMT Organization: Sun Microsystems Inc. Approved: clamage@eng.sun.com (comp.std.c++) Message-ID: <41t0sd$3ui@engnews2.Eng.Sun.COM> References: <1995Aug28.164444.14306@nlm.nih.gov> Reply-To: clamage@Eng NNTP-Posting-Host: taumet.eng.sun.com Content-Type: text X-Nntp-Posting-Host: taumet.eng.sun.com Content-Length: 2032 X-Lines: 48 Originator: clamage@taumet