Linux -Blue forest free software | Return to home page | Site Map | Search WWW | Contact Us |
Your current position : Homepage > Free Software > Technological exchanges >Application Programming


    

Blue Forest http://www.lslnet.com at 10:18 on June 6, 2006


GCC to #pragma pack (N) Support

Brother accidentally discovered in China GCC4 #pragma pack (8) seems to be wrong.
[code]#pragma Pack (8)
A struct (
The biggest 4003rd char 1
8 double 1997a 4003rd
};
[/code]
VC in the size of this structure is under 16 (under 8bytes filled), but in GCC4 (FC4) is under 12. I have consulted other friends were tested Linux and FreeBSD 3.2 + + GCC GCC 3.4.2, the are 12. MinGW 3.3 on the Windows platform is 16. In the official files had not found relevant information, Google see that the GCC supports 1, 2, 4, 8, 16 pragma pack (N).
Ariadne's heroes want to understand this situation. Thank you!

GCC to #pragma pack (N) Support

I was also 12 gcc3.3.5
A definition of temporary and if the two variables and print their addresses
They will discover more strange
Variables should occupy 16 bytes of space
But instead of a double on the normal use int
Double really strange

GCC to #pragma pack (N) Support

The GNU manual says.

Without a value specified, pack all structure members together without holes.
When a value is specified (which must be a small power of two), pack structure
Members according to this value, representing the maximum alignment (that
That is, objects with default alignment requirements larger than this will be output
Potentially unaligned at the next fitting location.

GCC to #pragma pack (N) Support

Azerbaijan not read this in English. . . .
[code]
#pragma Pack (16)

Typedef struct t1{
Char c;
Int days.
T1)
//#pragma Pack (1)

Main ()
{
T1 a, 1997a

Printf ( "%d\n", sizeof (a));
Printf ( "%d\n", sizeof (b));

Printf ( "%p\n" &a);
Printf ( "%p\n" &b);
}
[/code]
Output is
8
8
0xbfffed40
0xbfffed38

Why sizeof or eight?

GCC to #pragma pack (N) Support

Then a double issue
[code]
#pragma Pack (4)

Typedef struct t1{
Char c;
Double days.
T1)

Main ()
{
T1 a, 1997a

Printf ( "%d\n", sizeof (a));
Printf ( "%d\n", sizeof (b));

Printf ( "%p\n" &a);
Printf ( "%p\n" &b);
}
[/code]
Output is
12
12
0xbfffed30
0xbfffed20

Why is the space occupied variable b 16 bytes?

GCC to #pragma pack (N) Support

[quote= "Adacu" ]that is. objects with default alignment requirements larger than this will be output potentially unaligned at the next fitting location
GCC to #pragma pack (N) Support

"Is this pack ;>; also associated with the OS?

Said on the. Memory Alignment Alignment tacit rules in different ways and there is a difference between the system, GCC compiler on this point in the system is treated differently.

Although so far we have not seen the GCC in Linux use #pragma pack (N) memory alignment rules, but the results from my test is : tacit alignment is the type int (4 bytes) alignment, if designated #pragma pack (N) of N, N can not exceed a specified length of alignment acquiescence, acquiescence, if the alignment is four, the value of N can be 1, 2, 4, 4, as more than four after treatment. In this restriction does not seem to Windows systems.

To adacu :

You quoted -fpack-struct[=n] option is the use of the compiler. Without this premise, your note is not correct.

In addition, discussion is #pragma pack is a direct alignment of memory, you used to explain the contents of this is not the case, the landlord can explain the issues raised.

It is able to answer the question, 5th Floor
Using pack (4) Alignment
Then the definition of the two variables
Print addresses from the first of these two variables can be found
Even the space occupied by temporary variables is 16 bytes
This is why

-->

Here the "temporary variables" best known as "variable" or "local variables." In addition, the space occupied by each variable value sizeof decision, it should be 12 rather than 16. As for the difference between the two variables is the address of 16 instead of 12 shows that these two variables are not continuous, there are the "gap", it does not violate the standard C and C + +. As for the reasons for doing so could be the alignment of the structure memory address is the address variables in multiples of 16 addresses, thereby enhancing the efficiency of the structure of the visit.

If you define a structure of the above types of array elements adjacent then check the addresses will be found between adjacent elements is a continuous process.

Fruit so!!
High man
Bow



 Privacy Policy  Copyright © 1999-2000 LSLNET.COM. All rights reserved. Blue Forest website owners. E-mail : Webmaster@lslnet.com