GCC memory of their options, warning, otherwise it

*** gcc its option [-fpack-struct]

 gcc -fpack-struct[=n]

The compiler option to solve the byte alignment problem structure.

All -fpack-struct structure members compressed together without a gap. Because it makes greatly reduced, and the structure member offsets and system libraries do not match, do sometimes lead to addressing errors, it is generally not use this option code program efficiency.

original:

-fpack-struct[=n]
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 is, objects with default alignment requirements larger than this are output potentially unaligned at the next fitting location.

Warning: the -fpack-struct switch causes GCC to generate code that is not binary compatible with code generated without that switch. Additionally, it makes the code suboptimal. Use it to conform to a non-default
application binary interface.

 

*** gcc -Wpadded detects whether the byte alignment

#include<stdio.h>

struct my {

char *name;
int age;
} my_details;

int main() {
struct my person1;
return 0;
}

Compile

gcc -Wpadded -fpack-struct=8 -o test /tmp/test.c

You receive a warning, because int 4 bytes.

 

*** otherwise aligned

#ifdef __cplusplus
extern "C" {
#endif

// The following code is used for byte alignment
#ifndef the WIN32
#ifndef __GNUC__
#define __PACKED__
#pragma packed The Options align = left =
#else
#define __PACKED__ the __attribute__ ((packed The))
#pragma Pack (Push,. 1)
#endif
#else
__PACKED__ #define
#pragma Pack (Push,. 1)
#endif

 

***********************

struct TestStruct4
{
char a;
long b;
};

***********************

 

#ifndef WIN32
#ifndef __GNUC__
#pragma options align=reset
#else
#pragma pack(pop)
#endif
#else
#pragma pack(pop)
#endif

 

#ifdef __cplusplus
}
#endif

or:

#pragma pack (8)

struct TestStruct4
{
char a;
long b;
};

#pragma pack ()

Using instructions #pragma pack (n), the compiler will be aligned in n bytes.
Using instructions #pragma pack (), the compiler will cancel custom byte alignment.

But the file must follow the C way, using extern "C" {} option.

 

**** Other online summary

http://blog.chinaunix.net/uid-25808509-id-2426501.html?_t=t

Note that other #pragma pack of other uses:
#pragma pack (the Push) // Save the current on its way to Stack Packing
#pragma pack (the Push, the n-) is equivalent to #pragma pack (the n-)
#pragma pack (the n-) / / n = 1,2,4,8,16 saves the current alignment is provided by the n-byte alignment
#pragma pack (pop) // packing stack out of the stack, the stack and set the alignment of an embodiment thereof

[Pack] results related to OS
default memory alignment and justification rules on a different memory systems are different, so the GCC compiler at this point is treated differently on different systems.
Although we have not seen GCC under Linux system using #pragma pack (N) memory alignment rules,
but from the results of my tests look like this: The default alignment is aligned to an int (4 bytes), If the specified #pragma pack (N), then the N, N default alignment can not exceed a specified length, i.e., if the default alignment is 4, then, the value of N may be 1, 2,4, 4 processing as after more than four.
On Windows systems do not have this restriction seems.

 

Guess you like

Origin www.cnblogs.com/20170722-kong/p/12129053.html