Quote:
Originally Posted by rstewart
Why not use the pack pragma to set the alignment around the structure? IE:
|
I have seen some very convincing discussions by gcc experts on why
#pragma is worse than
__attribute__ for all the things that can be accomplished either way.
I don't remember the details well enough to repeat any of it convincingly, but I was convinced when I read it.
In real projects, it is a lot easier to wrap
__attribute__ into a preprocessor macro (to make it properly conditional on the compiler/architecture in a semi portable project) than to do that with #pragma
Quote:
Originally Posted by rajesh180186
struct s2 {
char a;
int i __attribute__ ((packed));
char b;
int i1;
};
|
I can't tell whether any of the answers to the reopening of this thread directly answer the question (vs. assume the answer). So my version of that answer is:
In the quoted code, the packed attribute applies only to
i and not to
i1
Other methods of specifying packed (appearing elsewhere in this thread) apply the packed attribute to the entire struct.
Quote:
Originally Posted by NevemTeve
But please note that this packed stuff:
- is a non-standard extension
|
That is important to understand when using it.
Quote:
- slows down the processing
|
That is only sometimes true at all and is rarely significant.
That claim should only be made about fundamentally flawed features, which this certainly is not. This is a very useful extension to C++, which should be used where appropriate. Like any other advanced language feature, it should not be scattered into your code without understanding or valid purpose. But that certainly doesn't mean it should be avoided.