How to get a byte offset

Bill Poser poser at csli.Stanford.EDU
Tue May 29 04:51:51 AEST 1990


In article <16802 at phoenix.Princeton.EDU> pfalstad at phoenix.Princeton.EDU (Paul John Falstad) writes:
>
>The first way is just to add up all the sizeof values of a, b, and c.
>
>  #define D_OFFSET (sizeof(int)+sizeof(char)*3+sizeof(int)*10)

This won't work in general since, in order to satisfy alignment requirements,
a struct may contain padding bytes. For example, on some machines the
structure:

	struct foo{
		char b;
		short a;
	};

will contain 4 bytes because it will be laid out in memory like this:

	0000	b
	0001	PADDING
	0002	low byte of a
	0003	high byte of a

Without the padding a would not begin on an even address, which
is not permitted in some architectures.
							Bill



More information about the Comp.lang.c mailing list