结构体对齐

来源:互联网 发布:死亡女神 漫威 知乎 编辑:程序博客网 时间:2024/06/05 06:56

看了很多博客写的不是太明白。


中文资料网址:

http://blog.csdn.net/ouzhf/article/details/5859886

http://blog.csdn.net/beyond_cn/article/details/6149828
http://blog.csdn.net/cfeibiao/article/details/6791865

http://www.360doc.com/content/10/0504/19/1265113_26088047.shtml

http://blog.chinaunix.net/space.php?uid=8464637&do=blog&id=2461146


两个英文网址解释的还不错

网址1:http://bytes.com/topic/c/answers/543879-what-structure-padding    

网址2:http://clinuxpro.com/structure-padding          


网址1的内容:

Structure Padding

Most processors require specific memory alignment on variables certain types. Normally the minimum alignment is the size of the basic type in question, fo instance this is common

char variables can be byte aligned and appear at any byte boundary

short (2 byte) variables must be 2 byte aligned, they can appear at any even byte boundary. This means that 0x10004567 is not a valid location for a short variable but 0x10004566 is.

long (4 byte) variables must be 4 byte aligned, they can only appear at byte boundarys that are a multiple of 4 bytes. This means that 0x10004566 is not a valid location for a long variable but 0x10004568 is.

Structure padding occurs because the members of the structure must appear at the correect byte boundary, to achieve this the compiler puts in padding bytes (or bits if bit fields are in use) so that the structure members appear in the correct location. Additionally the size of the structure must be such that in an array of the structures all the structures are correctly aligned in memory so there may be padding bytes at the end of the structure too

struct example {
char c1;
short s1;
char c2;
long l1;
char c3;
}

In this structure, assuming the alignment scheme I have previously stated then

c1 can appear at any byte boundary, however s1 must appear at a 2 byte boundary so there is a padding byte between c1 and s1.

c2 can then appear in the available memory location, however l1 must be at a 4 byte boundary so there are 3 padding bytes between c2 and l1

c3 then appear in the available memory location, however because the structure contains a long member the structure must be 4 byte aligned and must be a multiple of 4 bytes in size. Therefore there are 3 padding bytes at the end of the structure. it would appear in memory in this order

c1
padding byte
s1 byte 1
s1 byte 2
c2
padding byte
padding byte
padding byte
l1 byte 1
l1 byte 2
l1 byte 3
l1 byte 4
c3
padding byte
padding byte
padding byte

The structure would be 16 bytes long.

re-written like this

struct example {
long l1;
short s1;
char c1;
char c2;
char c3;
}

Then l1 appears at the correct byte alignment, s1 will be correctly aligned so no need for padding between l1 and s1. c1, c2, c3 can appear at any location. The structure must be a multiple of 4 bytes in size since it contains a long so 3 padding bytes appear after c3

It appears in memory in the order

l1 byte 1
l1 byte 2
l1 byte 3
l1 byte 4
s1 byte 1
s1 byte 2
c1
c2
c3
padding byte
padding byte
padding byte

and is only 12 bytes long.

I should point out that structure packing is platform and compiler (and in some cases compiler switch) dependent.


Memory Pools are just a section of memory reserved for allocating temporarily to other parts of the application

A memory leak occurs when you allocate some memory from the heap(or a pool) and then delete all references to that memory without returning it to the pool it was allocated from. 



网址2的内容:

Structure padding in C

Structure padding is adding extra bits at the end of the structue,so that the structure completes the word boundary.

How structure padding done

Structure Padding is done to do memory alignment. As size of pointer is 4 bytes, if everything is organized by multiple of 4, that it will be easier and faster to calculate the address and processing them. As structures are used to club different size variables, compiler will align to 4 byte boundaries and for that it needs padding.

Structure padding is done by the compilers and this depends on the architectures. Some architectures cannot access the data which will be stored on the odd addresses or they may find difficult to access it. This is the reason for padding extra bytes.

Padding will be done by compiler to structure’s members and to the structure as a whole also. Compiler pads structure as whole because this allows each member of structure aligned in array of structures.

Structure padding example

Struct EX
{
Char a;
Short int b;
Char c;
Long d;
};

Due to structure padding this structure size is
12 bytes (a=1 + 1 padding=2 + b=2 + c=1 + padding=3 + d=4) insted of 8 bytes (1+2+1+4).

Structure padding in C
a
b
c
d
char(1)Padding(1)short int(2)char(1)Padding(3)long(4)

Packed structures

Some compilers provide #pragma to suppress padding or to make it packed to n number of bytes. Some provide keywords to do this.

#pragma pack(n)

The keyword __attribute__ allows you to specify special attributes of variables or structure fields. This keyword is followed by an attribute specification inside double parentheses

So a packed structure is a structure without padding.
Generally packed structures will be used
• to save space, and
• to format a data structure to transmit over network using some protocol (this is not a good practice of course because you need to deal with endianness).

Structure packing in GCC

The packed attribute specifies that a variable or structure field should have the smallest possible alignment one byte for a variable, and one bit for a field, unless you specify a larger value with the aligned attribute.

Here is a structure in which the field x is packed, so that it immediately follows a:

struct foo
{
char a;
int x[2] __attribute__ ((packed));
};

aligned (alignment)

This attribute specifies a minimum alignment for the variable or structure field, measured in bytes. For example, the declaration:

int x __attribute__ ((aligned (16))) = 0;

gcc supports an extension that allows structures or structure members to be marked with __attribute__((packed)), which tells gcc to leave out all padding between members. Sometimes you need this, when you want to make sure of the layout of a structure. For example, you might have something like

struct my_struct {
int field1;
char field2;
} __attribute__((packed));

Without the packed attribute, the struct will have padding between field1 and field2, and that’s no good if this struct is something that has to match hardware or be sent on a wire.

But adding __attribute__((packed)) goes further than just telling gcc that it should not add padding – it also tells gcc that it cannot make any assumptions about the alignment of accesses to structure members. And this leads to disastrously bad code on some architectures.

To see this, consider the simple code

struct foo { int a; };
struct bar { int b; } __attribute__((packed));


原创粉丝点击