473,385 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to portably check *char alignment?

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
3 4306
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Joseph Suprenant | last post by:
I have an array of unsigned chars and i would like them converted to an array of ints. What is the best way to do this? Using RedHat 7.3 on an Intel Pentium 4 machine. Having trouble here, hope...
2
by: derek.google | last post by:
I have an application that's crashing because of an alignment problem, and this is the smallest program that demonstrates what's happening: int main() { struct Message { unsigned short size;...
5
by: Roy Hills | last post by:
When I'm reading from or writing to a network socket, I want to use a struct to represent the structured data, but must use an unsigned char buffer for the call to sendto() or recvfrom(). I have...
17
by: James S. Singleton | last post by:
Let S be a pointer to a bytestring of length L. I would like to extract 4 bytes from S at the location p = S + d, with 0 < d < L - 4, and store them into an unsigned int. I am looking for...
2
by: ais523 | last post by:
I've been wondering about the compatibility of pointers to signed, unsigned and plain char, and it's not clear to me how interchangeable they are. Is the following portable? int main(int argc,...
33
by: Mark P | last post by:
A colleague asked me something along the lines of the following today. For some type X he has: X* px = new X; Then he wants to convert px to a char* (I'm guessing for the purpose of...
21
by: Hallvard B Furuseth | last post by:
Is the code below valid? Generally a value must be accessed through the same type it was stored as, but there is an exception for data stored through a character type. I'm not sure if that...
31
by: aarklon | last post by:
Hi all, this is a question which i saw in a book typedef struct mall_li_header_ { int refcnt; uchar pool; uchar flag; ushort magic_no; char data;
4
by: rorni | last post by:
Hi, I'm porting code from Windows to HP-UX 11.23, using gcc (I'm begining to suspect this might not be the best choice, but I thought it would be most compatible since we've already compiled on...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.