[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Programming question: sizeof struct?



%% Jason Gunthorpe <jgg@ualberta.ca> writes:

  jg> On 9 Jul 1999, Paul D. Smith wrote:

  jg> I always hated gcc __attribute__, I prefer the simpler and more common
  >> 
  jg> #pragma pack(1)
  jg> struct {};
  jg> #pragma pack()
  >> 
  >> Ugh.  #pragma is terrible.  See the GCC manual for a description of why.

  jg> Their two reasons are true but not really compelling.

Apparently you mean "in the case of #pragma pack" (given your arguments
below).  Above you appear to be dismissing __attribute__ in all its
incarnations and preferring #pragma.  Perhaps I misread your intent.

  jg> First off, #pragma pack is virtually a standard, I know of 5
  jg> compilers that support the exact semantics I gave, and secondly
  jg> you are not going to be using it in a macro in this context.

The real issue with not being able to have macros expand to #pragma is
the first point: there is no clean way to use #pragma in a portable
environment, because you can't use the preprocessor to choose #pragmas
for different platforms.  All you can do is have massive amounts of
#ifdefs.

While it's good that this works with many compilers, that doesn't mean
it works for all or, even worse, that all of them implement it the same
way.  Suppose you _did_ need to support a compiler that required a
different format of arguments to the pack #pragma, for example: how
would you do that?

Since you can't macro-ize it in any way, you'd be reduced typing:

 #ifdef ODD_PACK_STYLE
 # pragma pack(0,1)
 #else
 # pragma pack(1)
 #endif

_every_time_ you wanted to use it!  Ouch!  And what if you had 3, 4, or
more different formats?

No, IMO #pragma is a very badly conceived solution.  Very badly indeed.

  jg> The big benifit that you do get is that it is done right, if you
  jg> miss an alignement directive where you needed one then 'woops'.

Sorry, it's Monday; I can't parse this sentence properly.  The big
benefit WRT what?  What does this buy you that __attribute__ doesn't in
terms of missing an alignment directive?
 
  jg> Sadly every compiler has a gcc-like 'innovation' there are so many
  jg> different ways to specify alignement <sigh>

It would be nice if there was One (guaranteed) Way, I agree.  However,
on the grand scale of things I'd say a very, very small percentage of C
programs ever need anything like this (that's probably why it's not a
high priority in the standards organization).

-- 
-------------------------------------------------------------------------------
 Paul D. Smith <psmith@baynetworks.com>         Network Management Development
 "Please remain calm...I may be mad, but I am a professional." --Mad Scientist
-------------------------------------------------------------------------------
   These are my opinions---Nortel Networks takes no responsibility for them.


Reply to: