Padding and rearranging structure members

May 11, 2009

Dan_Saks-May 11, 2009

A couple of months ago, I showed how a C++ delete-expression is actually a two-step process: (1) apply a destructor to the object in the allocated storage, and (2) deallocate the storage.1 In an array-delete-expression, the first step is actually a loop, which applies the destructor to each array element.

When I described the implementation of the loop, I omitted the details of how a delete-expression determines the array dimension. I promised to present those details--and I will--but not just yet. The details draw upon an understanding of memory alignment and the use of padding in structures, which are my topics this month.

Many of today's processors can address memory one 8-bit byte at a time. They can also access memory as larger objects such as 2- or 4-byte integers, 4-byte pointers, or 8-byte floating-point numbers.

Multibyte objects often have an alignment. The C Standard defines alignment as a "requirement that objects of a particular type be located on storage boundaries with addresses that are particular multiples of a byte address".2 The Standard leaves it up to each target processor to specify its alignment requirements. That is, a processor might require that a 4-byte integer or pointer referenced as a single object be word aligned--at an address that's a multiple of four. A processor also might require that an 8-byte floating-point number be word aligned, or maybe even double-word aligned--at an address that's a multiple of eight.

According to the C Standard, a program that attempts to access an improperly aligned object produces undefined behavior. This means that the program is in error, but the exact consequences of that error are platform-dependent. With many processors, an instruction that attempts to access improperly aligned data issues a trap. With other processors, an instruction that accesses misaligned data executes properly but uses up more cycles to fetch the data than if the data were properly aligned.

An object whose address requirement is a higher multiple than another is said to have a stricter alignment. For example, an object that must be double-word aligned (at an address that's a multiple of eight) has a stricter alignment than an object that must be only word aligned (at an address that's a multiple of four). Character objects always have a size of one (by definition) and can reside at any boundary. They have no alignment requirement.

Machines with 4-byte words and 8-byte double words are very common but hardly universal. The following discussion uses these common sizes for illustrative purposes only. Please bear in mind that machines with other word sizes and alignment requirements do exist.

< Previous
Page 1 of 5
Next >

Loading comments...