Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Portable alignof

Reply
Thread Tools

Portable alignof

 
 
William Ahern
Guest
Posts: n/a
 
      03-08-2006
Thoughts, comments?

#ifdef HAVE_TYPEOF

/*
* Can use arbitrary expressions
*/
#define alignof(t) \
((sizeof (t) > 1)? offsetof(struct { char c; typeof(t) x; }, x) : 1)

#else

/*
* Can only use types
*/
#define alignof(t) \
((sizeof (t) > 1)? offsetof(struct { char c; t x; }, x) : 1)

#endif

 
Reply With Quote
 
 
 
 
Ben Pfaff
Guest
Posts: n/a
 
      03-08-2006
William Ahern <> writes:

> #define alignof(t) \
> ((sizeof (t) > 1)? offsetof(struct { char c; t x; }, x) : 1)


I'm not certain whether that's portably correct or not, but I
must say that, regardless, it's remarkably clever.
--
"What is appropriate for the master is not appropriate for the novice.
You must understand the Tao before transcending structure."
--The Tao of Programming
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      03-09-2006
William Ahern <> writes:
> Thoughts, comments?
>
> #ifdef HAVE_TYPEOF
>
> /*
> * Can use arbitrary expressions
> */
> #define alignof(t) \
> ((sizeof (t) > 1)? offsetof(struct { char c; typeof(t) x; }, x) : 1)
>
> #else
>
> /*
> * Can only use types
> */
> #define alignof(t) \
> ((sizeof (t) > 1)? offsetof(struct { char c; t x; }, x) : 1)
>
> #endif


I don't see a problem with it, but I don't think the test for
sizeof(t) is necessary:

#define alignof(t) \
offsetof(struct { char c; t x; }, x)

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
Peter Nilsson
Guest
Posts: n/a
 
      03-09-2006
William Ahern wrote:
> Thoughts, comments?
> ...
> #define alignof(t) \
> ((sizeof (t) > 1)? offsetof(struct { char c; t x; }, x) : 1)


Looks okay, but realise that whilst it will return a suitable
alignment,
it may not return the minimum alignment. For instance, a 4 byte
int may only require two byte alignment on a given machine, but
because of compiler dependant padding settings, alignof might
return 4 (or even 8 etc...).

Your test if sizeof(t) > 1 would indicate that you have already
encountered such an issue with char in that regard.

If you're paranoid and you want a little more precision, you might
want to take the minimum of your expression and sizeof(t)...

#define min(a,b) ((a) < (b) ? (a) : (b))

#define simple_alignof(t) \
(sizeof (t) > 1 ? offsetof(struct { char c; t x; }, x) : 1)

#define alignof(t) min(sizeof(t), simple_alignof(t))

--
Peter

 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      03-09-2006
Ben Pfaff wrote:
> William Ahern <> writes:
>
>> #define alignof(t) \
>> ((sizeof (t) > 1)? offsetof(struct { char c; t x; }, x) : 1)

>
> I'm not certain whether that's portably correct or not, but I
> must say that, regardless, it's remarkably clever.


And it should be efficient, generating a simple constant. This
should have appeared in the making fatal assumptions thread, where
just that sort of problem led to various assumptions.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>


 
Reply With Quote
 
pemo
Guest
Posts: n/a
 
      03-09-2006
William Ahern wrote:
> Thoughts, comments?
>
> #ifdef HAVE_TYPEOF
>
> /*
> * Can use arbitrary expressions
> */
> #define alignof(t) \
> ((sizeof (t) > 1)? offsetof(struct { char c; typeof(t) x; },
> x) : 1)
>
> #else
>
> /*
> * Can only use types
> */
> #define alignof(t) \
> ((sizeof (t) > 1)? offsetof(struct { char c; t x; }, x) : 1)
>
> #endif


Could someone provide an example of how to use this please - and the typeof
macro if poss?


--
==============
Not a pedant
==============


 
Reply With Quote
 
Jordan Abel
Guest
Posts: n/a
 
      03-09-2006
On 2006-03-09, pemo <> wrote:
> Could someone provide an example of how to use this please - and the typeof
> macro if poss?


typeof isn't a macro, it's a non-standard keyword. That's why it's
guarded by a HAVE_TYPEOF macro.
 
Reply With Quote
 
REH
Guest
Posts: n/a
 
      03-09-2006

Keith Thompson wrote:

> I don't see a problem with it, but I don't think the test for
> sizeof(t) is necessary:
>
> #define alignof(t) \
> offsetof(struct { char c; t x; }, x)
>
> --


Isn''t the test needed because, with the above, alignof(char) will
yield 2?

REH

 
Reply With Quote
 
REH
Guest
Posts: n/a
 
      03-09-2006

William Ahern wrote:
> Thoughts, comments?
>
> #ifdef HAVE_TYPEOF
>
> /*
> * Can use arbitrary expressions
> */
> #define alignof(t) \
> ((sizeof (t) > 1)? offsetof(struct { char c; typeof(t) x; }, x) : 1)
>
> #else
>
> /*
> * Can only use types
> */
> #define alignof(t) \
> ((sizeof (t) > 1)? offsetof(struct { char c; t x; }, x) : 1)
>
> #endif


I think it's very clever. I am wondering, though, what is the
necessity of typeof? I assume that if it is available, you want to use
it. That would imply it somehow makes its form of alignof "better" but
I cannot tell why. please explain.

REH

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      03-09-2006
"REH" <> writes:
> Keith Thompson wrote:
>
>> I don't see a problem with it, but I don't think the test for
>> sizeof(t) is necessary:
>>
>> #define alignof(t) \
>> offsetof(struct { char c; t x; }, x)
>>

> Isn''t the test needed because, with the above, alignof(char) will
> yield 2?


Nope. Given struct { char c; char x; }, c has offset 0 and x has
offset 1 (unless the compiler decides to insert padding between c and
x).

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Portable Python - free portable development environment ! perica.zivkovic@gmail.com Python 7 01-13-2007 11:19 AM
Operator: alignof Frederick Gotham C++ 39 07-03-2006 11:14 PM
portable (VHDL) vs. non-portable (altera LPM) approaches to signed computations Eli Bendersky VHDL 1 03-01-2006 02:43 PM
Logitech MM22 Portable iPod Speakers @ BonaFideReviews Silverstrand Front Page News 0 08-05-2005 03:07 AM
Ultra Mini Portable Disk Enclosure Review @ PC Modding Malay Silverstrand Front Page News 0 07-01-2005 01:33 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57