Re: Question about Declare and Type
From: Jim Mack (jmack_at_mdxi.nospam.com)
Date: 12/24/04
- Next message: Jim Mack: "Re: Question about Declare and Type"
- Previous message: Brian Schwartz: "Re: Merry Christmas"
- In reply to: Jim Carlock: "Re: Question about Declare and Type"
- Next in thread: face: "Re: Question about Declare and Type"
- Reply: face: "Re: Question about Declare and Type"
- Messages sorted by: [ date ] [ thread ]
Date: Fri, 24 Dec 2004 15:57:01 -0500
Jim Carlock wrote:
> Maybe I'm confused now.
>
> I was thinking that a structure defined inside of C with pack(4)
> means that if the structure ends up with 3 bytes, an extra byte
> would be padded onto the END of the STRUCTURE. I
> don't think it gets the padding inside each unit of the structure,
> because that would break the structure, wouldn't it?
>
> I mean if you want three different bytes inside of a structure,
> you'd want:
>
> Private Type tagTest
> Byte1 As Byte
> Byte2 As Byte
> Byte3 As Byte
> End Type
>
> and in C:
>
> #pragma pack(4)
> typedef struct NetThreeByteMask {
> UCHAR byte1;
> UCHAR byte2;
> UCHAR byte3;
> }
>
> I always thought the extra byte was added at the end of the
> structure, rather than three bytes added to each variable in
> the structure.
>
> Which is correct?
Really, neither, but this isn't a good example.
In this particular case, no padding is needed within the struct, except
that if you declare an array of such, each element of the array will
have a byte of padding between it and the next element, because the
array itself must align elements on 4-byte boundaries, per OLE rules.
Padding is applied within a struct, _before_ an element, when that
element would not otherwise align to its natural size, up to the size
given in the pack() pragma. That's why placing larger elements before
smaller ones will give the optimum packing for the struct. Since the
natural size of a char or byte is 1, no padding is ever needed before a
byte within a struct. This is called "natural 4-byte alignment" (8-byte
by C default).
The OP is confused because he seems to think that #pragma pack(4) means
that each element will occupy a minimum of 4 bytes, which is
demonstrably not the case. What it really means is that every element
of 4 bytes or larger will start at an address that is a multiple of 4;
every element of 2 bytes or larger will start at an even address, and a
1-byte element may start at any address. Pack(8) simply extends that co
ncept upward by one notch.
There is a special case of this for VB "String * N" structure elements,
because they're normally composed of 2-byte (Unicode) characters in
memory, but one-byte characters when passed to a Declared function --
they're converted to MBCS (ANSI) characters. So the alignment within a
struct can vary from its definition, when passed to a C function.
That's why the best advice is to replace all "String * N" elements with
static byte arrays.
--
Jim Mack
MicroDexterity Inc
www.microdexterity.com
- Next message: Jim Mack: "Re: Question about Declare and Type"
- Previous message: Brian Schwartz: "Re: Merry Christmas"
- In reply to: Jim Carlock: "Re: Question about Declare and Type"
- Next in thread: face: "Re: Question about Declare and Type"
- Reply: face: "Re: Question about Declare and Type"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|