Re: bool vs. BOOL




"MrAsm" <invalid@xxxxxxxxxxx> wrote in message
news:orprq2ppvdc9uk2q9f4lugr51efcabktei@xxxxxxxxxx


On Wed, 17 Jan 2007 07:12:53 -0000, "David Webber"
<dave@xxxxxxxxxxxxxxxxxxx> wrote:

As someone paranoid about structure packing and byte boundaries in
structures,

Dave: what is this?

Do you mean that if structures are 4-bytes aligned, the code is more
efficient (because PC architectures are 32 bits? But on 64 bits
machine?)

I probably really meant paranoid :-) It isn't really a question of
efficiency - more control-freakery :-)

I use bit fields a *lot*. If I need packing bit fields I put them in
explicitly so that bit fields do not cross 2 byte boundaries (or 1byte if I
can help it). I tend to set 2 byte boundaries for packing.

More generally, I do not like invisible packing bytes anywhere, and tend
always to put a "reserved" variable in so that I can see the whole structure
as it will be in memory (and zero unused elements explicitly in the
constructor). It is a habit I have got into.

There are some very good underlying reasons.

The main one is that my music software has large arrays of small
elements representing notes, bar-lines etc, and that in a new version these
may acquire more bit fields (in place of hitherto reserved ones) without
changing the size of the structure. They are read in as just so many bytes
from the data file, and when reading an old format file, I can do it in the
sure knowledge that the new bit fields will be read in as zero.

It is a paranoia which has served me well in code maintenance an development
over nearly two decades! :-)

The result is that I would never (in 32 bit systems) write

struct ...
{
BYTE ...
short ...
int ...
BYTE ....
};

but rather

struct ...
{
int ...
short ...
BYTE ...
BYTE ....
};

It just sort of offends my sense of what is right to do it the first way :-)

It might be like not walking under ladders, but I suspect we all have our
comfort zones :-)

Dave
--
David Webber
Author MOZART the music processor for Windows -
http://www.mozart.co.uk
For discussion/support see
http://www.mozart.co.uk/mzusers/mailinglist.htm







.



Relevant Pages

  • Re: reading a C++ structure from a binary file using C#
    ... because it pragma pack was used several different values would be possible. ... The int is the first field of the struct, the second is a char array which has no alignment restriction, so, whatever the packing, the length written on disk will be 74. ... Padding is included in sizeof, because sizeof is the separation between adjacent array elements which must both be properly aligned. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: VB6 weirdness on W7
    ... prints a simple packing list,Saves the file as a simple text file.I'm ... have made no changes to the programme since it last worked,and made no ... W7x64 box and had used some 3rd party Genealogy software with no problems ... Now for some reason it seems to not save the data file occasionally. ...
    (microsoft.public.vb.general.discussion)
  • Re: Cross-platform way to pack (int + flags) to unsigned int
    ... int X and int F and assuming that ... I know that both packing and unpacking code is not valid from ISO C point of view but still the question is - should I care about it? ... values is perfectly safe. ...
    (comp.lang.c)
  • Re: scrunch data
    ... single int? ... And conversely decompose this int into a character in and a ... Packing is perhaps the term you are seeking. ...
    (comp.lang.java.help)
  • Cross-platform way to pack (int + flags) to unsigned int
    ... int X and int F and assuming that ... I know that both packing and unpacking code is not valid from ISO C point of view but still the question is - should I care about it? ...
    (comp.lang.c)