🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #4582

Re: Alignment of malloc()

#4582
From: Daniel Villeneuve
Date: Mon, 08 Jan 1996 14:57
36 lines
2115 bytes
Michael Spertus<mps@geodesic.com> writes:

> Therefore, any code using floats must align all floating point malloc()
> to avoid running with abysmal efficiency on many of the most popular
> C/C++ compilers.

This does solve the problem only for direct allocation of floats.  If the
compiler is unaware of performance hits when aligning floats on 4-byte
boundaries, it will only pad its structures on 4-byte boundaries.  So the
following structure:

struct {
  double d1;
  long   l1;
  double d2;
  long   l2;
};

will be packed (no padding) (assuming sizeof(long) == 4) and access to
either d1 or d2 will be misaligned, no matter the alignment of the struct.

This problem may be solved if all doubles are pushed at the beginning of
the structure, but there is no hint in the Standard why one should do so.

This remark was just to say that the alignment problem is not really a
problem with software, such as the semantics of malloc(), but a problem
with the machine architecture, because the struct above will have to be
translated with the same offsets by every compiler on a given architecture
(this is not a Standard's requirement, but a strong marketing one).

-----------------------------------------------------------------------
Daniel Villeneuve                       Tel: +1 514 340 6046
Graduate student in Operations Research Fax: +1 514 340 5665
GERAD/Mathematiques Appliquees          Email: danielv@crt.umontreal.ca
Ecole Polytechnique de Montreal
Technical Details
Message-ID: <DANIELV.96Jan8095711@lagrange.crt>
Path: arc1-ca.pugleaf.net!feed.umontreal.ca!usenet
References: <DKDA7D.Kw7@midway.uchicago.edu> <j66Sx*FRe@yaps.rhein.de> <DKKHCH.L6r@midway.uchicago.edu> <4ccbdb$5v6@fg70.rz.uni-karlsruhe.de> <DKo8ns.8A9@midway.uchicago.edu>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!feed.umontreal.ca!usenet
From: danielv@lagrange.crt (Daniel Villeneuve)
Newsgroups: comp.std.c
Subject: Re: Alignment of malloc()
Date: 08 Jan 1996 14:57:11 GMT
Organization: Universite de Montreal
Distribution: world
Message-ID: <DANIELV.96Jan8095711@lagrange.crt>
References: <DKDA7D.Kw7@midway.uchicago.edu> <j66Sx*FRe@yaps.rhein.de>
 <DKKHCH.L6r@midway.uchicago.edu> <4ccbdb$5v6@fg70.rz.uni-karlsruhe.de>
 <DKo8ns.8A9@midway.uchicago.edu>
Reply-To: danielv@crt.umontreal.ca
NNTP-Posting-Host: gate.crt.umontreal.ca
In-reply-to: Michael Spertus's message of Thu, 4 Jan 1996 19:26:16 GMT