Handling variable alignment

On all processors except IA-32, proper alignment of variablesis mandatory. While not mandatory on IA-32 processors, proper alignment significantlyenhances performance.

The remainder of this topic applies to processors other thanIA-32.

The compiler will not let you declare a variable which is notproperly aligned. All allocated memory coming from memory allocation will be properlyaligned. The only way you can get data which is not aligned is to have data in recordfields which is not aligned. The compiler defaults to a mode where it will pad recordfields to make sure each field is properly aligned. If this option (Packed records)is off then some fields may not be aligned. A compiler warning will be generatedif a field is not aligned. Arrays of unaligned records can be unaligned and a warningwill be generated.

The only reason to not let the compiler pad records for alignment, is because the record structure is written to disk and you must be backwards compatible with existing data. If a record is never written to disk then you should keep the default compiler option which guarantees data alignment. You may want to order your record fields to minimize memory waste due to padding, but this is just an efficiency consideration.

You have three options to handle alignment errors.

Operating system handles alignment errors

Some operating systems, probably all, will support the abilityto handle alignment errors. This support must be enabled via some system definedmechanism. This method will result in slower execution performance than the compilerspecial code generation (discussed below). It all depends on exactly what is misalignedand how it is used.

If you pass an unaligned address to the operating system, theoperating system will likely function, but then again it may not. It all dependson how the system implements the alignment error fixes.

Special compiler code generation

The compiler can generate special code for misaligned data viaa compiler option. This code generation is much larger and dramatically slower, butthis is to be expected.

If you need to use this option then you should use this optionin all source files, including the language runtime library project. This is becauseof reference parameters. Reference parameters have no way of knowing if they werepassed an aligned or unaligned address. Therefore they must assume an unaligned addressand generate the special code.

The compiler assumes that pointer types contain an "allocated" value. Remember that it is not possible to get an unaligned address via memory allocation. If you take the address of an unaligned record field and assign it to a pointer, that pointer access will fault with an alignment error.

If you pass an unaligned address to the operating system, theoperating system will fault using this address.

Fix the alignment errors in your code

Take your medicine and fix the source of the problem. The curefor non alignment errors is not entirely painless. The medicine may taste bad, butin the long run you are much better off.

Encapsulation

A simple method of dealing with a large installed base of dataon disk, which you do not want to convert, and which is not aligned, is to do thefollowing.

Encapsulate the reading and writing of the offending records. Preferably in a compilation unit created specifically for this task. Declare two versions of the offending record. One which is aligned and one which is not aligned. The aligned record exists in memory. The unaligned record is the one written to disk. Within these encapsulation module(s) you read/write the disk record and then assign the fields one by one to/from the memory record which is aligned. Use the compiler option to allow the unaligned fields on this compilation unit only. You could enable the compiler option per procedure. Since the in memory record is automatically padded by the compiler you never have to worry about alignment considerations, and your disk data structure has not changed in format.

Note: That this mechanism also provides an handy location toencapsulate and deal with big/little endian data format issues. Two for the priceof one,. Such a deal!!