By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,204 Members | 1,360 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,204 IT Pros & Developers. It's quick & easy.

How to portably check *char alignment?

P: n/a
I have some pretty heavily optimized old code written to be nice and
portable in a 32-bit environment.

In places, it needs to check the lowest few bits of various pointers
for alignment.

Now I am porting this code to 64-bit environments including MS and
gcc, and whatever modern and near-future systems my customers and
customers-to-be are likely to run on.

I need to cast pointers of arbitrary size to any arbitrary integer
class with at least 3 bits of length and then look at those few bits.

What are some good options to do this portably?

My best idea so far is to cast to a ptr_diff_t. Seems like that would
have to be an integer type which would have a strong tendency to be te
same size as the pointer type.

How big are the problems with that?

Any better ideas?

Thanks,
- Tim.

Aug 9 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Thu, 09 Aug 2007 11:08:25 -0700, tbroberg wrote:
I have some pretty heavily optimized old code written to be nice and
portable in a 32-bit environment.

In places, it needs to check the lowest few bits of various pointers
for alignment.

Now I am porting this code to 64-bit environments including MS and
gcc, and whatever modern and near-future systems my customers and
customers-to-be are likely to run on.

I need to cast pointers of arbitrary size to any arbitrary integer
class with at least 3 bits of length and then look at those few bits.

What are some good options to do this portably?

My best idea so far is to cast to a ptr_diff_t. Seems like that would
have to be an integer type which would have a strong tendency to be te
same size as the pointer type.
1) It is ptrdiff_t, not ptr_diff_t.
2) It isn't *guaranteed* to be able to hold a pointer. If you have
C99, intptr_t in <stdint.his (but it isn't guaranteed to exist).
--
Army1987 (Replace "NOSPAM" with "email")
No-one ever won a game by resigning. -- S. Tartakower

Aug 9 '07 #2

P: n/a
tb******@hifn.com writes:
I have some pretty heavily optimized old code written to be nice and
portable in a 32-bit environment.

In places, it needs to check the lowest few bits of various pointers
for alignment.

Now I am porting this code to 64-bit environments including MS and
gcc, and whatever modern and near-future systems my customers and
customers-to-be are likely to run on.

I need to cast pointers of arbitrary size to any arbitrary integer
class with at least 3 bits of length and then look at those few bits.
As far as I know there's a type intptr_t somewhere.
>
What are some good options to do this portably?
As far as I remember casting pointer into an integer is undefined or
unspecified or whatever behaviour so there is no portable way.

I guess you would be best creating a macro which would check the
alignment and redefine it for each platform your code have to work.
I suppose the following should work in most cases:

#define PTRALIGN(ptr) ((unsigned int)(ptr) & 7)

--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +--<mina86*tlen.pl>---<jid:mina86*chrome.pl>--ooO--(_)--Ooo--
Aug 9 '07 #3

P: n/a
On Aug 9, 12:13 pm, Army1987 <army1...@NOSPAM.itwrote:
1) It is ptrdiff_t, not ptr_diff_t.
2) It isn't *guaranteed* to be able to hold a pointer. If you have
C99, intptr_t in <stdint.his (but it isn't guaranteed to exist).
Ok, I've defined a type that all the pointers get cast to, and set the
type to intptr_t.

This seems to work fine.

Thanks to all for the quick and expert advice.
- Tim.

Aug 10 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.