one pragma pack vs. many, many __attribute__((packed))


one pragma pack vs. many, many __attribute__((packed))

Postby Steve French » Sat, 29 Oct 2005 04:50:10 GMT

Seems like a big annoyance, but gcc on some platforms (arm) will
generate different code for
	pragma pack(1)
vis the corresponding more verbose alternative ie specifying
on each structure one by one.  Presumably there are good reasons for why
packing them across the whole file does not mean you can necessarily
read them when they aren't aligned (my imagination is not that good)

Although it makes the code more verbose to individually call out packing
on each structure, I don't see any reasonable alternative that would
work (at least on arm) as I have to deal with structures coming in off
the network that are packed - and in some cases not aligned on 2 or 4
byte boundaries either.   special case parsing them off the wire (as
opposed to accessing them with their structure field names) would be
worse, and copying them into a temp buffer would be worse ... are there
no reasonable magic compile options or pragmas that will have the effect
of attribute__((packed)) without being so verbose?

To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to  XXXX@XXXXX.COM 
More majordomo info at   http://www.**--****.com/ 
Please read the FAQ at   http://www.**--****.com/ 

Similar Threads:

1.Why #pragma pack() not take effect?

Struct definition as following(on 32-bit Linux):

#pragma pack(push, 8)
struct MY_STRUCT
    char a[2];
    short b;
    short c;
    short d;
    int e;
    long long x;
    long long y;

During the test, result of 'sizeof(struct MY_STRUCT)' is 28. Why not 32?
As I had expected, a,b,c,d will be packed into one 8-byte, e one and x, y
two. Ain't I right?

If I get wrong usage of #pragma pack(), could anyone please tell me how to
get it work correctly?

BTW, what on earth is the difference between __attribute__ align() and
#pragma pack()?

Thanks in advance!

2.[PATCH] Fix pragma packing in ip2 driver

3.[News] New Service Pack for Ballnux and Windows XP Service Pack Dies

4.sigbux with __attribute__((packed)) on irix


I'm using IRIX 6.5 and gcc 2.95.2 (and I'd prefer not to upgrade ;-) ).
I have to use packed structs in my program (yes, I know I shouldn't.
But I have to). But the following program gives a SIGBUS:

#include <iostream>
using namespace std;

typedef struct Test
    short a;
    unsigned int b;
} __attribute__((packed)) Test;

void myBla(unsigned int* i)
    *i = ((*i)>>24) | (((*i)>>8) & 0xff00) | (((*i)<<8) & 0xff0000) |
int main()
    Test t = { 0x0100, 0x01000000 } ;
    myBla(&t.b); //<- BANG

    cout << sizeof(t) << " " << t.b << endl;
    cout << sizeof(short) << " " << sizeof(unsigned int) << endl;

Why is this? (And if the all to myBla() is uncommented, the output is
6 256
2 4
, and the 256 isn't right either).


5.Eliminate __attribute__ ((packed)) warnings for gcc-4.1 (whitespace-beautified version)

6. 2.6.18-mm1 tiacx-fix-attribute-packed-warnings for wlan

7. [PATCH 3/3] __attribute__((packed)) for the CAPI message structs

8. need for packed attribute

Return to linux


Who is online

Users browsing this forum: No registered users and 49 guest