[Fwd: Re: for and struct alignment]

From: Fons Rademakers (Fons.Rademakers@cern.ch)
Date: Wed Jul 23 1997 - 18:43:01 MEST


The remark concerning struct alignment is important
general information.

-- Fons.


attached mail follows:


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



This archive was generated by hypermail 2b29 : Tue Jan 04 2000 - 00:26:20 MET