Re: UDT memory alignment

From: Bill McCarthy (bill_mcc_at_<)
Date: 02/26/04


Date: Thu, 26 Feb 2004 21:25:12 +1100

Hi Ian,

Probably the simplest way to look at this, is instead of calling it "dword
alignment" call it pack 4, or just "padding"

In VB an element of length n in bytes, where n = 2^p, and p is the set of
positive integers such that 2^pmax <= dword. that is {0,1,2}, and hence n is
1 byte, 2 bytes, and 4 bytes. Types that are longer are considered to be
multiples of dwords. So for an UDT, the alignment is such that the whole
size of the UDT is a multiple of a DWord, and that each element is
positioned at an alignment that is a multiple of it's length, n. So for a
byte, they can be placed without the need for preceding padding. An
integer, whose length is 2 bytes in vb5 and 6, is placed at an offset of a
multiple of 2. So the integer's placement will always be a high word or a
low word in a 32 byte register. For a 32 bit integer type, that being a
Long in vb5 and VB6, the alignment is always a multiple of 4, meaning that
the actual value is always a full 32 bit register, and does not span
registers.

As you might be guessing or seeing now, the reason for this alignment, is
that it allows for fast access to the values in situ, rather than having to
copy from consecutive blocks into one block just to get a long value.

So, just to recap, the padding is before the elements in an UDT with
multiple elements, to ensure that the value of each element is easily
accessible in situ. In addition to this, UDT's are also padded out to
multiples of 4 bytes when stored in arrays, to ensure that the same applies
to the next item in the array.

HTH's

Bill.

"Ian" <notreadingyourcrap@hotmail.com> wrote in message
news:%23JG4GC6%23DHA.552@TK2MSFTNGP11.phx.gbl...
> That is exactly what I thought I would have to do. But it seems, at least
> in VB6, that order is irrelevant. The padding is the same regardless of
the
> order of the elements.
>
> > double word boundary.
> > So, when you are designing the elements of your type, you place those
> > requiring double word alignment first, those requiring word alignment
> > next and so on to keep the amount of padding to a minimum.
> > If you have arrays of stuff, you have to be doubly careful with your
> > design and if you have variable strings you are well and truly screwed!!
> > --
> > Jane Ransom in Lancaster.
> > If you need to email me for any other reason, put ransoms
> > at jandg dot demon dot co dot uk where you see ransom@deadspam.com
> >
> >
>
>



Relevant Pages

  • Re: problem with sizeof
    ... my compiler spits out a warning on the above ... Actually it is because they have different alignment requirements. ... aligned on a multiple of 1). ... the padding is located. ...
    (comp.lang.c)
  • Re: Alignment, Cast (more about structs...)
    ... alignment is the requirement that an object of a given type start ... at a multiple of some particular number of bytes. ... different reading methods are needed if the ECC bits ... But ok, I suppose they could be treated as padding bits, so short has ...
    (comp.lang.c)
  • Re: Data alignment problems with sizeof and new
    ... >I have a strange problem with a project involving classes with data alignment ... It appears that, in this particular project, the sizeof and new ... >operators are not taking the alignment padding into account when calculating ... >class with the first member being a dword size followed by doubles, ...
    (microsoft.public.vc.language)
  • Re: RosAsm Team is Still Making Excuses
    ... And what that means is that if you *must* pick only a single alignment ... dword is a good choice. ... What is RosAsm "working around?" ... A declaration section is a sequence of declarations in which all ...
    (alt.lang.asm)
  • Re: Alignment
    ... 2, double on multiple of 4, ints on even multiples of 8, pointers ... chars cannot require anything stricter than byte ... alignment for 'struct '. ...
    (comp.lang.c)