Re: 4 vs 8 alignment
Re: 4 vs 8 alignment
- Subject: Re: 4 vs 8 alignment
- From: David Fang <email@hidden>
- Date: Wed, 13 Dec 2006 11:32:26 -0500 (EST)
> >e.g.
> >struct foo { int x; char y; } __attribute__((aligned));
> >
> >I prefer __attribute__ over pragma for portability, but otherwise it may
> >just be a matter of taste.
>
> How is __attribute__ more portable than pragma pack? Is it not a GNU
> extension? I know pragma pack is supported by gcc, CodeWarrior, and
> Visual Studio.
Hi,
'Portable' wasn't the right term. Yes, it is a GNU (and others)
extension. I meant that it's easier to disable __attribute__(...) where
it is unsupported by #define-ing it to something empty, whereas #pragma
may not necessarily be "cancel-able", since they are typically
compiler-specific. Consider:
// suppose you do detect attribute support in an autoconf-fashion:
#if HAVE_ATTRIBUTE_FOO
#define __ATTRIBUTE_FOO__ __attribute__((foo))
#else
#define __ATTRIBUTE_FOO__
// no-effect
#endif
Then you can use __ATTRIBUTE_FOO__ in your source wherever you'd like,
without having to wrap its uses in more preprocessor conditionals.
This can be generalized to support other compiler's attribute formats.
However, with #pragma, you can't wrap around it with a preprocessor macro
like:
#define __ATTRIBUTE_FOO__ #pragma foo
as '#' has a special meaning to the preprocessor.
You'd end up having to write:
#if HAVE_ATTRIBUTE_FOO
#pragma foo
#endif
All over the place -- wherever such an attribute is desired. I've also
seen compilers reject unknown pragmas before, but it's been ages since
I've tried to used one.
So to me, it's a matter of *convenience* for making code portable with
help from the preprocesssor, not really portability itself. So yes, with
similar precautions, you can conditionally use pragmas to achieve
portability. If you have a nifty way of conditionally using #pragmas,
please also share.
Thanks,
Fang
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden