From: "John M. Aldrich" Newsgroups: comp.os.msdos.djgpp Subject: Re: Padding Question Date: Sat, 07 Dec 1996 12:01:48 -0800 Organization: Three pounds of chaos and a pinch of salt Lines: 48 Message-ID: <32A9CD2C.6BAE@cs.com> References: <32A810D9 DOT 366E9B4E AT m-net DOT arbornet DOT org> <32A85B15 DOT 4015 AT skygames DOT com> <32A8F16C DOT 596F AT cs DOT com> <32A91EFF DOT 6C6FD6F0 AT alcyone DOT com> Reply-To: fighteer AT cs DOT com NNTP-Posting-Host: ppp211.cs.com Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit To: Erik Max Francis DJ-Gateway: from newsgroup comp.os.msdos.djgpp Erik Max Francis wrote: > > John M. Aldrich wrote: > > > #pragma is an evil, hackish way to make gcc pack structures. The DJGPP > > Frequently Asked Questions list (v2/faq202b.zip), chapter 22.9, tells > > you exactly how to go about packing structs in DJGPP, and refers you to > > the gcc reference which details the syntax of the __attribute__() > > extension. > > I'm curious -- what's "unevil" or "unhackish" about using attributes to > pack structures? Both are completely unportable, but it seems to me that > the #pragma will work in the widest number of cases (i.e., taking into > account old gcc bugs). __attribute__ is supported on gcc 2.5 or later versions, and this can easily be tested for in your programs with a block like the following: #if defined(__GNUC__) && __GNUC__ >= 2 && __GNUC_MINOR__ >= 5 ... #endif Also, you can use a similar test to insert a #pragma pack if you are writing C++ and the version is less than 2.7.2.1. Or you can create the necessary packed structs using extern "C" {...} There is, as far as I know, no such easy way to test whether #pragma pack will work on any given compiler. I have read many statements by experienced programmers, all of whom say that #pragma is a very bad thing to rely upon in a portable program. Finally, nearly every compiler that aligns structs uses a different method to specify that they should be packed. If you depend on specific structure alignment to make your programs work, then you have no alternative but to find out how to pack structs on each possible 32-bit compiler, and write in portability code. This is unfortunate, but IMHO it's better to rely on a concrete, defined instance of a packing operation than a nebulous #pragma that may or may not be supported. -- John M. Aldrich, aka Fighteer I -----BEGIN GEEK CODE BLOCK----- Version: 3.1 GCS d- s+:- a-->? c++>$ U@>++$ p>+ L>++ E>+ W++ N++ o+ K? w(---) O- M-- V? PS+ PE Y+ PGP- t+(-) 5- X- R+ tv+() b+++ DI++ D++ G e(*)>++++ h!() !r !y+() ------END GEEK CODE BLOCK------