Re: UDT memory alignment
From: Bill McCarthy (bill_mcc_at_<)
Date: 02/26/04
- Next message: Larry Serflaten: "Re: VB6 vs VB .Net"
- Previous message: Bill McCarthy: "Re: UDT memory alignment"
- In reply to: Ian: "Re: UDT memory alignment"
- Next in thread: Ralph: "Re: UDT memory alignment"
- Messages sorted by: [ date ] [ thread ]
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
> >
> >
>
>
- Next message: Larry Serflaten: "Re: VB6 vs VB .Net"
- Previous message: Bill McCarthy: "Re: UDT memory alignment"
- In reply to: Ian: "Re: UDT memory alignment"
- Next in thread: Ralph: "Re: UDT memory alignment"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|