Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!watmath!clyde!cbatt!ucbvax!jade!eris!mwm From: mwm@eris.UUCP Newsgroups: comp.lang.c Subject: Re: Determing alignment of (char *) pointer Message-ID: <1876@jade.BERKELEY.EDU> Date: Wed, 10-Dec-86 06:38:31 EST Article-I.D.: jade.1876 Posted: Wed Dec 10 06:38:31 1986 Date-Received: Sun, 14-Dec-86 10:43:28 EST References: <1510@mit-trillian.MIT.EDU> <299@bms-at.UUCP> Sender: usenet@jade.BERKELEY.EDU Reply-To: mwm@eris.BERKELEY.EDU (Mike (Don't have strength to leave) Meyer) Organization: Missionaria Phonibalonica Lines: 30 In article <299@bms-at.UUCP> stuart@bms-at.UUCP (Stuart D. Gathman) writes: >In article <1510@mit-trillian.MIT.EDU>, newman@mit-trillian.MIT.EDU (Ron Newman) writes: >> In particular, I need to determine whether the pointer is 32-bit >> aligned before attempting to store a long by casting it to a (long *). > >Cast it to a (long *) and back again then see if it changed. > >char *p; >if ( (char *) (long *) p == p) . . . As has been pointed out several times, the C compiler isn't guaranteed to convert p to a valid long* for you. As hasn't yet been pointed out, many (most?) modern machines are perfectly happy to fetch a long from an odd boundary; you just pay a performance penalty for it. So even if your compiler is "correct" (n.b. - the quotes don't mean that I think this is really correct for C), it could still fail. Worse yet, some over zealous (and given current memory costs, I'd be tempted to say not very bright) language maintainer decides that memory is more precious than CPU cycles, your code could quit working between compiles. This brought up an interesting question: Given a char *p that is not aligned, what should the cast (long *) p return if it wants to return an aligned pointer? The long containing *p? The next long after *p? Any standards say anything? Thanx,