delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/09/19/03:07:15

Date: Thu, 19 Sep 1996 08:49:30 +0200 (IST)
From: Eli Zaretskii <eliz AT is DOT elta DOT co DOT il>
To: sime AT fly DOT cc DOT etf DOT hr
Cc: djgpp AT delorie DOT com
Subject: Re: sizeof(long double)==12
In-Reply-To: <51om9k$cof@bagan.srce.hr>
Message-Id: <Pine.SUN.3.91.960919084134.3485E-100000@is>
Mime-Version: 1.0

On 18 Sep 1996, S. Mikecin wrote:

> DJGPP has three floating-point (FP) data types: float (4 bytes), double (8
> bytes) and long double (12 bytes). Since x86 FPU supports ONLY
> 32-bit,64-bit and 80-bit floating-point numbers (and also 64-bit integer
> numbers), I see no reason why long double is of size 12, and not 10 bytes!
> It has nothing to do with alignment because it is also 12, even when I use
> #pragma pack(1). Is there any explanation?

It *is* for alignment reasons.  GCC wants long double to be aligned on
DWORD boundaries.  Since you can create arrays of long double, this
implies padding long doubles to 12 bytes, and `sizeof' must know this, or
else pointer arithmetics won't work.  Let's say you have the following
code: 

	long double *pld = ldbl_array;

	while (*pld != 0.)
	  pld += 5;

The line "pld += 5;" is translated by the compiler like this:

	pld = (long double *)( (char *)pld + 5*sizeof(long double) );

Now you can understand why `sizeof' must be consistent with the alignment 
of the array elements (the same goes for structs which are sometimes 
padded at the end).

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019