programming tools for Windows applications development
  Home  |   SwiftForth Archive  |   SwiftX Archive  |

C Structures

From: Mike Ghan <mikeghan_at_logix-controls.com>
Date: Mon, 16 Apr 2001 10:24:48 -0700

Wow! - if I'd only known then what I know now ... Allow me to
summarize this in a SwiftForth friendly manner (any comments
appreciated).

As Conrad Weyns pointed out (from MSDN "Storage and Alignment of
Structures")

> Every data object has an alignment-requirement. For structures, the
> requirement is the largest of its members. Every object is allocated
> an offset so that
>
> offset % alignment-requirement == 0

To me this implies that the Class data object (that is the address
return by ADDR) must be aligned on a boundary equal to the largest
member in the CLASS. If a class has VARIABLEs, then the Class
instance should be aligned on a 4 byte boundary. Rick - is this so?

MSDN goes on to state:

> Structure members are aligned on the size of the type
> or the /Zp[n] size, whichever is smaller. The default
> size is 4.

Assuming the default pack size of 4, a 16 bit word is aligned on
even-addresses, a 32 bit word on 4 byte boundaries, a double-word (64
bits) also on 4 byte boundaries.

As John Whitt suggested, it would be useful to have a set of SWOOP
words to handle padding automatically for API structures: DWORD SHORT
BYTE.

The offsetof macro that Conrad presented appears to be a useful tool
for discussing Win structures. Let me expand on an example:

struct FOO
{
  BYTE f1;
  DWORD f2;
  BYTE f3;
  BYTE f4;
  DWORD f5;
};

offsetof(FOO, f2) == 4.
f2 is a 32 bit word and aligns on a 4 byte boundary. The structure is
padded with 3 bytes after f1.

offsetof(FOO, f3) == 8.
f3 is a byte and aligns on a byte boundary. No padding required.

offsetof(FOO, f4) == 9.
f4 is a byte and aligns on a byte boundary. No padding required.

offsetof(FOO, f5) == 12.
f5 is a 32 bit word and aligns on a 4 byte boundary. The structure is
padded with 2 bytes after f4.

The following from Conrad is also informative

sizeof(char) == 1
sizeof(short) == 2
sizeof(int) == 4
sizeof(long) == 4
sizeof(float) == 4
sizeof(double) == 8
sizeof(BYTE) == 1
sizeof(WORD) == 2
sizeof(DWORD) == 4
sizeof(LPARAM) == 4

(unsigned equivalents have identical size).

There are, of course, many more.

Mike

----------------------------------------------------------------------
sftalk_at_forth.com The SwiftForth programming discussion email list
To unsubscribe, send subject "unsubscribe sftalk" to listar_at_forth.com
For help with listar commands, send subject "help" to listar_at_forth.com
Archives are located at http://www.forth.com/sftalk -- check them out!
Received on Mon Apr 16 2001 - 10:26:40 PDT

This archive was generated by hypermail 2.2.0 : Thu Nov 20 2008 - 03:04:18 PST