Packed Strcutures in GCC

Programming, for all ages and all languages.
Post Reply
Friend

Packed Strcutures in GCC

Post by Friend »

Hi,

I need to know how I can create a packed structure in GCC, ie. it only has as many bytes as needed.

thanks.
Okiesmokie

Re:Packed Strcutures in GCC

Post by Okiesmokie »

typedef struct {
/* Code here */
} __attribute__((packed));
sonneveld

Re:Packed Strcutures in GCC

Post by sonneveld »

make sure you're not using this packed structure to read in data from a file.. cause it's very iffy. you're better off just reading in each byte to make sure you have the correct endian.

- Nick
Friend

Re:Packed Strcutures in GCC

Post by Friend »

With this would I put the name of the structure before __attribute__ ((packed)) like below?

typedef struct {
/* Code here */
} struct_name __attribute__((packed));

thanks.
Okiesmokie

Re:Packed Strcutures in GCC

Post by Okiesmokie »

I dont think it really matters, i put my struct names after the __attribute__((packed))
Tim

Re:Packed Strcutures in GCC

Post by Tim »

Also, this seems to work on both gcc and the Microsoft compiler:

Code: Select all

#pragma pack(push, 1) /* use 1, 2, 4, etc. */
struct your_structure
{
};
#pragma pack(pop)
Post Reply