[ROOT] Problem: The size of the C-structure vs sizeof()

From: Valeri Fine (Faine) (fine@bnl.gov)
Date: Wed May 24 2000 - 17:22:26 MEST


We have discovered a problem with Cint.
In our code we defined a pointer to C structure 
as follows:

  g2t_hits_st *row;

 we expected:
  int(row+1) - int(row) = sizeof(g2t_hits_st)

 But in real ROOT/Cint session we found 
  
root4star [20] int(row+1) - int(row)
(const int)60

root4star [14] sizeof(g2t_tpc_hit_st)
(int)52

As result the "for" loop over the array of that struct
was screw up:

for (int i=0;i<n;i++,row++) { . . . }

Note: the array was allocated by "compiled" code
and the real sizeof() is very 52 bytes.

We tried to reproduce this with a simple macro:
{
struct g2t_tpc_hit_st {
        long    id; 
        long    next_tr_hit_p;
        long    track_p;
        long    volume_id;
        float   de;
        float   ds;
        float   p[3];
        float   tof;
        float   x[3];
};

g2t_tpc_hit_st r;
g2t_tpc_hit_st *row = &r;
printf("sizeof(*row) = %d ;sizeof(g2t_tpc_hit_st) \n",sizeof(*row),sizeof(g2t_tpc_hit_st));
printf("(row+1)=%d;(row)=%d , diff = %d\n",int(row+1),int(row),int(row+1)-int(row));
}

and got:

root [3] .x size.C
sizeof(*row) = 56 ;sizeof(g2t_tpc_hit_st)
(row+1)=5493400;(row)=5493344
root [4] .x size.C
sizeof(*row) = 56 ;sizeof(g2t_tpc_hit_st)
(row+1)=5493400;(row)=5493344 , diff = 56


The same code been compiled with VC++ gives:

sizeof(*row) = 52 ;sizeof(g2t_tpc_hit_st)
(row+1)=1245052;(row)=1245000 , diff = 52

Sun:

sizeof(*row) = 52 ;sizeof(g2t_tpc_hit_st)
(row+1)=1245052;(row)=1245000 , diff = 52

Linux g++:

[rcas6025] ~/expert > a.out
sizeof(*row) = 52 ;sizeof(g2t_tpc_hit_st) 
(row+1)=-1073747128;(row)=-1073747180 , diff = 52

Is it a bug or feature ?

   Valery


root4star [15] row  
(struct g2t_hits_st*)0x9748c7c

root4star [16] row++
(struct g2t_hits_st*)0x9748c7c

root4star [17] row
(struct g2t_hits_st*)0x9748cb8

root4star [18] 0x9748cb8-0x9748c7c 
(const int)60

root4star [19] 


=================================================================
Dr. Valeri Faine (Fine)
    -------------------          Phone: +1 516 344 7806
Brookhaven National Laboratory   FAX  : +1 516 344 4206
Bldg. 510A /STAR                 mailto:fine@bnl.gov
Upton, New York, 11973-5000      http://nicewww.cern.ch/~fine
USA
                                 
Dr. Valery Fine                  Telex : 911621 dubna su
    -----------
LCTA/Joint Inst.for Nuclear Res. Phone : +7 09621 6 40 80
141980 Dubna, Moscow region      Fax   : +7 09621 6 51 45
Russia                           mailto:fine@main1.jinr.dubna.su                              



This archive was generated by hypermail 2b29 : Tue Jan 02 2001 - 11:50:26 MET