struct padding avoidance

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
User avatar
jean
Posts: 489
Joined: Sat Jan 05, 2008 2:44 am

struct padding avoidance

Post by jean »

I would like to set up a struct and avoid alignment in it. According to a website:
Although the standard didn't provide any control in customizing the alignment and padding rules, many compilers provide this through non-standard extensions.
But in wikipedia i found:
...some compilers use #pragma directives to specify packing inside source files...

Code: Select all

#pragma pack(push)  /* push current alignment to stack */
#pragma pack(1)     /* set alignment to 1 byte boundary */
 
struct MyPackedData
{
    char Data1;
    long Data2;
    char Data3;
};
 
#pragma pack(pop)   /* restore original alignment from stack */
The above directives are available in compilers from Microsoft, Borland, GNU and many others.
Just to stop those already writing "use search", this has nothing to do with __attribute__((aligned(16))) and i'm not experimenting directly the above code because my app has unexpected behaviours at the moment and i would not recognize the changes. If someone already had the same problem and wish to save me time writing some test code, then feel free to tell me if "#pragma pack(1) " works properly, or otherwise what's the pspsdk replacement if any. Thanks for your attention.
moonlight
Posts: 567
Joined: Wed Oct 26, 2005 7:46 pm

Post by moonlight »

Code: Select all

typedef struct MyStruct
{
    ...
} __attribute__((packed)) MyStruct;
That's what gcc uses.
Post Reply