int always 32 bits on all platforms?

AJ aj at nospam.net
Wed Oct 21 16:24:12 PDT 2009


"Jason House" <jason.james.house at gmail.com> wrote in message 
news:hbo2ru$2p6j$1 at digitalmars.com...
> AJ Wrote:
>
>>
>> "BCS" <none at anon.com> wrote in message
>> news:a6268ffbadb8cc20772570e4f0 at news.digitalmars.com...
>> > Hello aJ,
>> >
>> >> How can/does D guarantee that "int" will always be 32 bits on all
>> >> platforms? Does this mean that D won't work on some platforms?
>> >
>> >
>> > D is not built for 8 or 16 bit systems. However 32 bit math can be done 
>> > on
>> > a 16 bit CPU, it's just slow. In the other direction, 32 bit math (and 
>> > 16
>> > and 8 bit) can be done on a 64 bit CPU so that's not a problem.
>>
>> I wasn't thinking about math issues, but rather struct field
>> alignment/portability, and platform alignment requirements for basic 
>> types,
>> issues. I'm guessing that it is all worked out by the compiler, so that
>> makes the development of a compiler more difficult, if so, but how much 
>> more
>> difficult? A simple example or description of how this is done would 
>> really
>> be great. (BTW, this is definitely an area where I would approve of more
>> language implementation complexity for the great strides in programming
>> efficiency (and fun!) it gives).
>>
>>
>>
>
> In D, both type size and alignment have the same defaults on all 
> platforms.

That must be some kind of magic considering that only some platforms require 
alignment on certain (word, dword, etc.) boundaries.

> If desired, alignment can be explicitly controlled with the align 
> attribute. Similarly, use of specific types control sizes.

Tell me, in D, can I have all the members of the following struct be 
byte-aligned and still access and use the members directly on all platforms 
without some kind of  "seqfault"(?):

struct ABC
{
    byte a;
    int b; // may be improperly aligned on some platforms
    int64 c; // same issue
};

I don't see how the answer could be "yes" to the above. Now the following 
stands a chance at working across platforms (with D, not with C/C++) though:

struct CDE
{
    int32 c;
    int32 d;
    int64 e; // properly aligned, probably
};

So did I just "summarize" the extent to which guaranteed basic type widths 
can be exploited, in that the design of ABC is always a "no no" and the 
design of CDE is very likely to work reliably across a wide range of 
designated platforms? 





More information about the Digitalmars-d mailing list