lwip-users
[Top][All Lists]
Advanced

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

Re: [lwip-users] [PATCH] fix warning for gcc and possible unaligned acce


From: Derek Guerdon
Subject: Re: [lwip-users] [PATCH] fix warning for gcc and possible unaligned access
Date: Thu, 27 Apr 2006 08:30:40 -0400

Timmy Brolin wrote:

>Well, there was a long discussion about that two years ago. According to 
>C standards adatatype may not have alignment requirements larger than 
>its natural alignment, and padding may be introduced into structs to 
>algin the fields in the struct. This would suggest that as long as we 
>design the structs to be perfectly aligned, no compiler will add extra 
>padding.

I think this is a misinterpretation of the C standard. Although a data
type's alignment must be less than or equal to the size of the data
type, there is no requirement for compilers to lay out structures
using minimum alignment (i.e. structures need not use the fewest
possible number of padding bytes).

As an example, here is a type of case mentioned by the standard:

You have two structures:
struct foo { int i; char c[NC_FOO]; };
struct bar { int i; char c[NC_BAR]; };

The standard says that offsetof(foo, c) is not guaranteed to be equal
to offsetof(bar, c) unless NC_FOO == NC_BAR.


Given the freedom that the standard allows compilers in using padding
bytes, there is no real way to design a structure that is guaranteed
not to have padding bytes. 

-- 
Derek Guerdon





reply via email to

[Prev in Thread] Current Thread [Next in Thread]