using sizeof do get the number of bytes used by a type 
Author Message
 using sizeof do get the number of bytes used by a type

Hi,
I'm using the sizeof() function in dvf6 to compute the number of byte used
for a variable of a type defined in a very short program (see below)

I declare in this example 4 types
(a16byte,a12byte,a16byteagain,bitmapinfoheader)

The results are mysterious to me... can anyone tell me why does sizeof()
give

    16 instead of 14 for a variable of type a16byte
    12 instead of 10 for a variable of type a16byte
    16 instead of 12 for a variable of type a16byteagain

And in the mean time returns

    40  as expected for  a variable of type bitmapinfoheader ?

Thanks for helping
Antoine

_______________program follows______________

program szof

 implicit none

 TYPE a16byte !I expect sizeof() to return 14 and not 16
  integer*2::bfType
  integer*4::bfSize
  integer*2::bfRes1
  integer*2::bfRes2
  integer*4::bfOffBits
    END TYPE a16byte

 TYPE a12byte !I expect sizeof() to return 10 and not 12
  integer*2::bfType
  integer*4::bfSize
  integer*4::bfOffBits
    END TYPE a12byte

 TYPE a16byteagain !I expect sizeof() to return 12 and not 16
  integer*2::bfType
  integer*4::bfSize
  integer*2::bfRes2
  integer*4::bfOffBits
    END TYPE a16byteagain

 TYPE bitmapinfoheader !40 bytes as expected
  integer*4::biSize
  integer*4::biWidth
  integer*4::biHeight
  integer*2::biPlanes
  integer*2::biBitCount
  integer*4::biCompression
  integer*4::biSizeImage
  integer*4::biXPel{*filter*}eter
  integer*4::biYPel{*filter*}eter
  integer*4::biClrUsed
  integer*4::biClrImportant
    END TYPE bitmapinfoheader

    type (a16byte) :: v16byte
    type (a12byte) :: v12byte
    type (a16byteagain) :: v16byteagain
    type (bitmapinfoheader) :: bmpinfhead

 write(6,*) 'v16byte ',sizeof(v16byte)
 write(6,*) 'v12byte ',sizeof(v12byte)
 write(6,*) 'v16byteagain ',sizeof(v16byteagain)
 write(6,*) 'bmpinfhead ',sizeof(bmpinfhead)

end program szof



Mon, 29 Sep 2003 18:33:56 GMT  
 using sizeof do get the number of bytes used by a type
You're seeing the effect of padding. If your structures were to follow the
natural alignment rule (start with the largest items first, and then all
other items in order of decreasing size), your expectations would be met.
"bitmapinfoheader" just so happens to fill the gaps properly. You might
also make these SEQUENCE types, which may or may not change this behaviour.

        Jan



Mon, 29 Sep 2003 18:43:40 GMT  
 using sizeof do get the number of bytes used by a type

Quote:

> You're seeing the effect of padding. If your structures were to follow the
> natural alignment rule (start with the largest items first, and then all
> other items in order of decreasing size), your expectations would be met.
> "bitmapinfoheader" just so happens to fill the gaps properly. You might
> also make these SEQUENCE types, which may or may not change this behaviour.

I have used SEQUENCE and with some compilers it makes a difference, with others it
doesn't.

The structure padding thing is not unique to fortran. I have the same "problem" with
structures in IDL.

cheers,

paulv

--
Paul van Delst           A little learning is a dangerous thing;

Ph: (301)763-8000 x7274  There shallow draughts intoxicate the brain,
Fax:(301)763-8545        And drinking largely sobers us again.



Mon, 29 Sep 2003 21:34:47 GMT  
 using sizeof do get the number of bytes used by a type
On Thu, 12 Apr 2001 12:33:56 +0200, "Antoine Leimgruber"

Quote:

>Hi,
>I'm using the sizeof() function in dvf6 to compute the number of byte used
>for a variable of a type defined in a very short program (see below)

>I declare in this example 4 types
>(a16byte,a12byte,a16byteagain,bitmapinfoheader)

>The results are mysterious to me... can anyone tell me why does sizeof()
>give

>    16 instead of 14 for a variable of type a16byte
>    12 instead of 10 for a variable of type a16byte
>    16 instead of 12 for a variable of type a16byteagain

Jan is correct - you are seeing the results of padding.  If you add
SEQUENCE, you won't get padding in Compaq Fortran.  Actually, without
SEQUENCE, the compiler is free to rearrange the components in any
order, and with any spacing, it sees fit.



Fortran Engineering
Compaq Computer Corporation, Nashua NH

Compaq Fortran web site: http://www.compaq.com/fortran
Message Board: http://www.compaq.com/fortran/forum



Mon, 29 Sep 2003 22:11:40 GMT  
 using sizeof do get the number of bytes used by a type

Quote:

> Jan is correct - you are seeing the results of padding.  If you add
> SEQUENCE, you won't get padding in Compaq Fortran.  Actually, without
> SEQUENCE, the compiler is free to rearrange the components in any
> order, and with any spacing, it sees fit.

From what I understood of the discussion with respect to the standard,
SEQUENCE forces the compiler to generate a layout that is indepedent of
local considerations, i.e., it has to be the same for indepedantly compiled
units - but the compiler is still free to do any re-arrangements it sees
fit. Without SEQUENCE, the actual layout might depend on local considerations,
e.g., members that are never used or stored to can be optimized away, or
similar things.

        Jan



Tue, 30 Sep 2003 00:35:53 GMT  
 using sizeof do get the number of bytes used by a type
Yes you were all true, it is a padding thing and... it solved my problem !
I used a sequence statement and had also to add a compiler option:
/align:norecords to be sure that the compiler does align components on
arbitary byte boundaries. Both steps are required on dvf
With this, my program works smoothly.
Thanks a lot
Antoine


dans le message :

Quote:

> > Jan is correct - you are seeing the results of padding.  If you add
> > SEQUENCE, you won't get padding in Compaq Fortran.  Actually, without
> > SEQUENCE, the compiler is free to rearrange the components in any
> > order, and with any spacing, it sees fit.

> From what I understood of the discussion with respect to the standard,
> SEQUENCE forces the compiler to generate a layout that is indepedent of
> local considerations, i.e., it has to be the same for indepedantly
compiled
> units - but the compiler is still free to do any re-arrangements it sees
> fit. Without SEQUENCE, the actual layout might depend on local
considerations,
> e.g., members that are never used or stored to can be optimized away, or
> similar things.

> Jan



Tue, 30 Sep 2003 01:26:46 GMT  
 using sizeof do get the number of bytes used by a type



Quote:

> > Jan is correct - you are seeing the results of padding.  If you add
> > SEQUENCE, you won't get padding in Compaq Fortran.  Actually, without
> > SEQUENCE, the compiler is free to rearrange the components in any
> > order, and with any spacing, it sees fit.

> From what I understood of the discussion with respect to the standard,
> SEQUENCE forces the compiler to generate a layout that is indepedent of
> local considerations, i.e., it has to be the same for indepedantly
compiled
> units - but the compiler is still free to do any re-arrangements it sees
> fit. Without SEQUENCE, the actual layout might depend on local
considerations,
> e.g., members that are never used or stored to can be optimized away, or
> similar things

Correct except for what you say about rearrangements.  The SEQUENCE
attribute is supposed to be sufficient to allow a derived data type to be
EQUIVALENCEd or associated through COMMON with a set of
data of corresponding types.  However, the usual rules for EQUIVALENCE
and COMMON apply: non-numeric data cannot be associated with numeric
data (or even in the same common block), and non-default-kind numeric
data cannot be associated with default-kind numeric data (or even be in
the same common block).

Given those rules though, if you have a derived type containing only numeric
data of default KINDs, the SEQUENCE attribute will prevent either padding
or rearrangement.  To do otherwise would make that type incompatible with
COMMONs or EQUIVALENCEs with ordinary numeric variables.  Padding
of a SEQUENCE type might still be possible for non-default KINDs and for
CHARACTER values of strange lengths - but only if they would be similarly
padded if they appeared in a COMMON.  And, no rearrangement is allowed
at all.

--
J. Giles



Tue, 30 Sep 2003 01:42:51 GMT  
 using sizeof do get the number of bytes used by a type
On 12 Apr 2001 18:35:53 +0200, Jan Vorbrueggen

Quote:

>From what I understood of the discussion with respect to the standard,
>SEQUENCE forces the compiler to generate a layout that is indepedent of
>local considerations, i.e., it has to be the same for indepedantly compiled
>units - but the compiler is still free to do any re-arrangements it sees
>fit. Without SEQUENCE, the actual layout might depend on local considerations,
>e.g., members that are never used or stored to can be optimized away, or
>similar things.

Not quite.  The standard says this:

"By default, no storage sequence is implied by the order of the
component definitions.  However, if the definition of a derived type
contains a SEQUENCE statement, the type is a *sequence type*. The
order of the component definitions in a sequence type specifies a
storage sequence for objects of that type."

Without SEQUENCE, and assuming that the derived type object is not
accessed by other program units, a compiler could certainly "optimize
away" unused components.  The only way you are allowed to access
components is by referring to them directly.

But with SEQUENCE, the order is fixed and, if you don't mix numeric
and character, and don't use POINTER components, you have either a
"numeric sequence type" or a "character sequence type", which must
obey the sequence association rules.  I'm not 100% certain that
padding would not be allowed with SEQUENCE types, but our
implementation doesn't pad with SEQUENCE (for a while, it did - that
was a bug).  The standard allows more flexibility with SEQUENCE types
- allowing them in COMMON, for instance, where sequence association
rules also apply.



Fortran Engineering
Compaq Computer Corporation, Nashua NH

Compaq Fortran web site: http://www.compaq.com/fortran
Message Board: http://www.compaq.com/fortran/forum



Tue, 30 Sep 2003 01:52:23 GMT  
 using sizeof do get the number of bytes used by a type


Quote:

>> Jan is correct - you are seeing the results of padding.  If you add
>> SEQUENCE, you won't get padding in Compaq Fortran.  Actually, without
>> SEQUENCE, the compiler is free to rearrange the components in any
>> order, and with any spacing, it sees fit.

>From what I understood of the discussion with respect to the standard,
>SEQUENCE forces the compiler to generate a layout that is indepedent of
>local considerations, i.e., it has to be the same for indepedantly compiled
>units - but the compiler is still free to do any re-arrangements it sees
>fit. Without SEQUENCE, the actual layout might depend on local considerations,
>e.g., members that are never used or stored to can be optimized away, or
>similar things.

>    Jan

Your analysis is correct for sequence types other than character
sequence types and numeric sequence types.  For those two special
forms of sequence types, the rules on storage association force
the fields to be laid out in order.  The rules on storage
association don't strictly forbid padding character sequence
types and numeric sequence types, but for most machines it is
impractical to use padding in those types.

                                        Sincerely,
                                        Bob Corbett



Tue, 30 Sep 2003 07:34:21 GMT  
 using sizeof do get the number of bytes used by a type
However, if it is not <too> much effort, or there is not an absolute
necessity for the present alignment, it <may> be worthwhile to fix
alignment on word boundaries from a performance standpoint.
Quote:

> Yes you were all true, it is a padding thing and... it solved my problem !
> I used a sequence statement and had also to add a compiler option:
> /align:norecords to be sure that the compiler does align components on
> arbitary byte boundaries. Both steps are required on dvf
> With this, my program works smoothly.
> Thanks a lot
> Antoine


> dans le message :


> > > Jan is correct - you are seeing the results of padding.  If you add
> > > SEQUENCE, you won't get padding in Compaq Fortran.  Actually, without
> > > SEQUENCE, the compiler is free to rearrange the components in any
> > > order, and with any spacing, it sees fit.

> > From what I understood of the discussion with respect to the standard,
> > SEQUENCE forces the compiler to generate a layout that is indepedent of
> > local considerations, i.e., it has to be the same for indepedantly
> compiled
> > units - but the compiler is still free to do any re-arrangements it sees
> > fit. Without SEQUENCE, the actual layout might depend on local
> considerations,
> > e.g., members that are never used or stored to can be optimized away, or
> > similar things.

> > Jan



Tue, 30 Sep 2003 23:23:04 GMT  
 using sizeof do get the number of bytes used by a type



Quote:
> However, if it is not <too> much effort, or there is not an absolute
> necessity for the present alignment, it <may> be worthwhile to fix
> alignment on word boundaries from a performance standpoint.

Yes, but the purpose of my program was to write a bitmap file and the header
has to be 14 bytes when written to the actual bmp file. If not, then no
other program will recognize the file as in bitmap format. I do not know
though if it is possible to use a 16 bytes type and then write only the 14
'usefull' bytes to the file.


Thu, 02 Oct 2003 00:00:19 GMT  
 using sizeof do get the number of bytes used by a type
That's why I put the caveat(s) in there...if there's a reason, there's a
reason...
Quote:



> > However, if it is not <too> much effort, or there is not an absolute
> > necessity for the present alignment, it <may> be worthwhile to fix
> > alignment on word boundaries from a performance standpoint.

> Yes, but the purpose of my program was to write a bitmap file and the header
> has to be 14 bytes when written to the actual bmp file. If not, then no
> other program will recognize the file as in bitmap format. I do not know
> though if it is possible to use a 16 bytes type and then write only the 14
> 'usefull' bytes to the file.



Thu, 02 Oct 2003 22:41:31 GMT  
 using sizeof do get the number of bytes used by a type

Quote:



>> However, if it is not <too> much effort, or there is not an absolute
>> necessity for the present alignment, it <may> be worthwhile to fix
>> alignment on word boundaries from a performance standpoint.

>Yes, but the purpose of my program was to write a bitmap file and the header
>has to be 14 bytes when written to the actual bmp file. If not, then no
>other program will recognize the file as in bitmap format. I do not know
>though if it is possible to use a 16 bytes type and then write only the 14
>'usefull' bytes to the file.

Build a character string of the desired length, one item at a time, using TRANSFER,
and write it down to the file.

Michel

--

 http://www.ifremer.fr/metocean/group/michel/michel_olagnon.htm
 http://www.fortran-2000.com/



Sat, 04 Oct 2003 15:03:04 GMT  
 
 [ 13 post ] 

 Relevant Pages 

1. Using Python arrays of bytes for ILU SEQUENCE OF BYTE -- API is inadequate

2. 10 byte float numbers from ST 8 byte numbers

3. tcl objc type: args uses only string of own type

4. SIZEOF, sizeof() and parenthesis

5. SIZEOF SIZEOF

6. RB uses CPU even when doing nothing

7. Clarion Browse using SQL doing a SELECT ASC and then SELECT DESC

8. Doing continuos integration using Labview

9. Doing Sums using Prolog!

10. Using -use and -container - not quite doing what I want

11. any1 done a magic square in c++ using class

12. Has anyone done server-client using lisp-java?

 

 
Powered by phpBB® Forum Software