[Fwd: Re: for and struct alignment]

Fons Rademakers (Fons.Rademakers@cern.ch)
Wed, 23 Jul 1997 18:43:01 +0200


This is a multi-part message in MIME format.

--------------77A22DA34430
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

The remark concerning struct alignment is important
general information.

-- Fons.

--------------77A22DA34430
Content-Type: message/rfc822
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

Received: from dxmint.cern.ch (dxmint.cern.ch [137.138.26.76]) by hpsalo.cern.ch with ESMTP (8.7.1/8.7.1) id OAA07311 for <rootdev@hpsalo.cern.ch>; Tue, 22 Jul 1997 14:33:30 +0200 (METDST)
Received: from inetnif.niftyserve.or.jp (uucp@inetnif.niftyserve.or.jp [192.47.24.129]) by dxmint.cern.ch
with ESMTP id OAA15733 for <rootdev@hpsalo.cern.ch>; Tue, 22 Jul 1997 14:33:29 +0200 (MET DST)
Received: (from uucp@localhost) by inetnif.niftyserve.or.jp (8.8.4+2.7Wbeta4/3.5Wpl1-970106) id VAA13474; Tue, 22 Jul 1997 21:34:12 +0900 (JST)
Message-Id: <199707221234.VAA13474@inetnif.niftyserve.or.jp>
Date: Tue, 22 Jul 1997 21:23:00 +0900
From: =?ISO-2022-JP?B?GyRCOGVGIyEhQDU8IxsoQg==?= <MXJ02154@niftyserve.or.jp>
Subject: Re:for and struct alignment
To: A.Lavrentev@gsi.de, rootdev@hpsalo.cern.ch
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-2022-jp

Dear Anton Lavrentev,
INET:A.Lavrentev@gsi.de

>1)
>for (int i = 0; ; i++) {
> // Something
>}
>This loop never executes. If I put 1 instead of empty space
>between two semicolons, I get the loop working.
>C++ specifies that empty condition in the 'for'-loop
>is treated as a true one. Isn't it in case of CINT?

This is a bug. I knew for(;;) causes infinit loop, but overlooked
the case that only condition field is emply. I'll fix this.

>2) Suppose I have the following declaration
>struct s {
> char c;
> short b;
>};
>and I want to map the array of this structures on something
>I have in some buffer. The usual way is to cast pointers.
>s *p = (s *) buffer;
>and then
>printf("%c, %d\n",p[i].c,p[i].b);
>But this obvious program doesn't work properly. I get
>some missed entries from buffer. Why? Because
>sizeof(s) gives me 8 instead of sizeof(char) + sizeof(short)
>which is 1 + 2 = 3 !
>I tried to declare different structures and I always
>got sizeof(structure) equal to multiple of 8 inspite of
>contents. I don't know whether this is a feature of CINT
>or a bug. Please make the situation clear.

I guess contents of the buffer is generated by a compiled code.
Each compiler or interpreter has own alignment rule. CINT has different
alignment rule from others. CINT always aligns size of class/struct to
integer multiple of sizeof(double). The reason why is portability. CINT
should work on arbitrary processor with arbitrary compiler where
alighment rule is different. Some environment trigger a bus error when
address of the object is not integer multiple of its' size. I have no
way to find out alignment and bus error rule of everything. What I did
instead was to use the most conservative alignment rule. In case of
this example, sizeof(s) can be 4 instead of 8. I can make the alighment
rule a little smarter, but still can not guarantee the matching to all
compilers.

So, please remember the rule below.

1) If struct/class is precompiled, CINT mirrors compiler's alignment
rule.
2) If struct/class is interpreted, CINT uses unique alignment rule
which is very conservative and does not match that of compiler.

Masaharu Goto

--------------77A22DA34430--