tags:

views:

61

answers:

2

"whether a field may overlap a word bounday is implementation - defined.fields need not be named; unnamed fields ( a colon and width only) are used for padding. the special width 0 may be used to force alignment at the next word boundary."

i m unable to get these lines...please explain...

A: 

First it mostly has to do with memory 'aligment'. Compilers often align variables or fields on word boundaries, a word is 32bits on a 32 bit platform. This means that two bools will bethe first byte in different words, rather than two consecutive bytes.

Bit fields can force a layout in memory: you can be sure a specific field only uses 3 bits if its values range from 0-7.

A field can be unnamed. You don't need to name fields if you're not going to use it. This may be used to force a specific layout.

If you use :0, it will auto align on the next word boundary.

In general, you don't need this behavior unless you're tuning performance in some way.

Stephen
A: 

Basically, if an address is "word aligned", the processor can perform operations faster. A word is 32 bits generally, or 4 bytes.

Typical processors are "word" aligned, meaning that they can retrieve an entire "word" of memory in one operation. When a value is across multiple values, the processor must perform multiple operations to get the same data. Sometimes, this is unavoidable for instance if you are using a "double word" but if you have a single word that stretches across a word boundary, the CPU will have to perform 2 operations to retrieve the single word of data.

An example of a word aligned value is 0x10000004, 0x10000008. Since a word is 4 bytes, the address must be a divisible by 4. A non word aligned value is 0x10000003.

To the programmer, all operations will work as expected, but under the hood, the CPU must perform 1 memory operation to read or write to 0x10000004 whereas it must perform 2 memory operations to read or write to 0x10000003, since it crosses a word boundary.

In reference to your original question, this is basically saying that depending on the compiler you use, the compiler may or may not word align your fields. This is an example of size vs speed, as you can pack more data if you don't word align, but as shown above, it will be slower.

samoz