Page 1 of 1

Packed Strcutures in GCC

Posted: Fri Oct 25, 2002 11:26 am
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.

Re:Packed Strcutures in GCC

Posted: Fri Oct 25, 2002 2:04 pm
by Okiesmokie
typedef struct {
/* Code here */
} __attribute__((packed));

Re:Packed Strcutures in GCC

Posted: Fri Oct 25, 2002 5:20 pm
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

Re:Packed Strcutures in GCC

Posted: Fri Oct 25, 2002 8:45 pm
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.

Re:Packed Strcutures in GCC

Posted: Sat Oct 26, 2002 11:20 am
by Okiesmokie
I dont think it really matters, i put my struct names after the __attribute__((packed))

Re:Packed Strcutures in GCC

Posted: Wed Oct 30, 2002 7:54 am
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)