how to disable structure alignment using gcc compiler options ?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
This will set the compiler to pack structures in 1 byte chunks (no fillers)
#pragma pack(pop)
will get you back to default.
Edit -- Sorry, I replied to quickly. Yes, you are correct. The size of individual datatypes change. long in 32bit = 4bytes, 8 bytes in 64bit OS from my experience. If you cannot change the structure to use variables with set sizes (uint32_t for ex.), then you need to d/l the 32-bit libs (Google is your friend here, as I cant recall the names of the libs req'd, buildessentials is one--*I think*) and compile with the -m32 flag to get 32bit object code (and hence datatypes).
it is basically both, the data type sizes (long equals 4bytes on 32bit vs 8bytes on 64bit) change AND the alignment requirements change as well. 4-byte data types are aligned on a 4-byte boundary, while 8-byte data types are aligned on an 8-byte boundary. So, I doubt that packing would help here. By the way, just for completeness and to avoid the #pragma directives of the compiler, you could do the following to specify the "pack" attribute per structure definition:
[CODE]
struct a
{
...
} __attribute__((packed));
But note, the __attribute__ syntax is a GNU extension which you wouldn't find on any other compiler.
Lastly, why do you think your application will crash due to the port to 64-bit architecture? How do you fill your structures? If you would like to, you could post your structure and extracts of your code which you think will crash ...
If your structure represents a hardware layout, then you can force the sizes of components of the structure using the predefined types from stdint.h (such as uint16_t etc), or by using the ':n' notation, eg
Code:
unsigned int foo:16;
Both the packing and alignment can be set using the packed and aligned attributes as demonstrated in the above posts.
However, you should be careful of such controlled structures; they are seldom portable (such as when moving between little endian to big endian architectures), and often represent a failure to serialize data. The best reason for packing structures is to conserve space (usually at the expense of speed), not to force a consistent byte format.
Last edited by neonsignal; 01-26-2010 at 08:43 AM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.