[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Unaligned trap in smail...

Oscar Levi <elf@buici.com> writes:

> I have found the problem in smail that causes an unaligned trap.  It
> is a case of a data structure containing a pointer, eight bytes long,
> where the address of that structure member does not have all three low
> bits zero.  I wonder if there is a standard method for solving this
> kind of alignment problem.  I'm gonna look into the code more to see
> where the data is allocated.  It may be as simple as making the whole
> structure be an even number of 64 bit words long.

Normaly the compiler should care about alignment.

struct foobar {
	int foo;
	long bar;

should give the following:

0: -- struct foobar
0: foo
4: pad
8: bar
16: -- sizeof(foobar)

sizeof(foobar) should be 16.

I don't know what happens if somebody does a malloc. Can anybody tell?

s = (foobar *)malloc(16);  // This might give 4
                           // or is it aligned to 8 or 16?

s->foo could then be 4, which is the correct 32 Bit alignment, but
s->bar would then be 12, which is BAD.

What alignment does the kernel and the gcc give for memory
allocations? I guess the kernel does 8K alignment, i.e. full pages,
but what about gcc?

Also what alignment gives a malloc(12);?
Whats the size of sizeof(barfoo);?

struct barfoo {
	long bar;
	int foo;

May the Source be with you.

Reply to: