answersLogoWhite

0

Structure alignment relates to the way objects in memory must be allocated according to the underlying architecture. For instance, a machine might expect an int to be aligned upon a 4-byte boundary. If that is the case then a structure with a member int must be aligned upon a 4-byte boundary and the int member offset from that boundary by some multiple of 4 bytes.

Consider the following:

struct X_ { char c; int i;

} X;

assert (sizeof(X)==sizeof(int)+sizeof(char)); /* oops */

Assuming sizeof(int) is 4 and sizeof(char) is 1 and that an int must be aligned on a 4-byte boundary, the actual length of this structure must be 8 bytes, not the 5 we expected. If it were anything other than 8, then an array of such structures would have misalignments in 3 out of every 4 elements. Note that in order to correctly align this structure, the compiler must insert 3 padding bytes immediately after the char member. This ensures that the int member is offset 4 bytes from the start of the structure. So long as the structure itself is aligned upon a 4-byte boundary (which it now will be because 8 is a multiple of 4), the int will always be correctly aligned.

Inserting padding bytes between members can often increase a structure's size far more than we might expect, particularly if there are many members. To avoid this, it is best to declare members in order of size, largest first. In this way, whenever padding is required, the padding is more likely to be placed at the end of the structure rather than between two or more members.

struct X_ {

char c;

int i;

char c2;

} X;

Given the same criteria as before, this structure would be 12 bytes in length. But by changing the order of the members, we can reduce this to just 8 bytes:

struct X_ {

int i;

char c;

char c2;

} X;

The reason this works is because a char can always be aligned on a 1-byte boundary, so two consecutive char members will easily fit inside a 4-byte word. Thus we only require 2 bytes of padding instead of 4.

Note that it is never safe to assume the lengths of any data type in C other than char which is always sizeof(char)==1. The lengths of all other types are measured in terms of a char, but will vary from system to system according to the underlying architecture. Always use the sizeof() operator to determine type lengths. Note that the sizeof() operator computes data type lengths at compile time, so there is no runtime cost, nor is there any need to store the return value.

User Avatar

Wiki User

9y ago

What else can I help you with?

Related Questions

Why need a structure padding in c?

to satisfy structre member alignment rules structure padding needed when alignment matters, which is useful for performance and avoids potential bus errors when alignment is a must on a particular architecture, please refer http://www.refcode.net/2013/02/structure-padding-explained.html for detailed explanation


What is mineral alignment?

the alignment of a mineral structure


How are structure elements stored in memory in c?

Structure elements in C are stored in memory contiguously based on the order in which they are declared in the structure definition. The elements are typically aligned to memory addresses that are multiples of their size to optimize access speed. The size of the structure is determined by the sum of the sizes of its individual elements, possibly with padding to ensure proper alignment.


How can you use the word alignment in a sentence about computers?

Data structure alignment is becoming much more advanced in computer these ways.


How do you determine the size of a structure?

Add up the sizes of the individual pieces, plus the slack space between them that might be there (depending on the computer, compiler, and options) for memory alignment purposes. In C or C++, you can usually use the sizeof() operator to determine this.


How do you calculate the size of the structure?

The Structure size can be calculated by adding the memory requirements of each individual structure members. This is applicable for the word alignment of one byte. The word alignment of one byte can be set by the following statement#pragma pack(1).AnswerDon't try to calculate anything, use sizeof.


What do you mean by non primitive data structure in C programming?

What do you mean by searching in data structure in C.?


Basic control structure available in c plus plus?

The basic control structure in C++ is the if statement.


What is structure and union?

Structure is a query that is used in language c++


What is the ideal c/a ratio for a crystal structure?

The ideal c/a ratio for a crystal structure is typically around 1.633.


How do you create modules in c language?

by using structure in c.........


A b c d side of structure?

The a b c d side of a structure at an incident are the exposures or surrounding properties