Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!ucsd!sdd.hp.com!zaphod.mps.ohio-state.edu!wuarchive!uunet!mcsun!ukc!dcl-cs!aber-cs!athene!pcg From: pcg@cs.aber.ac.uk (Piercarlo Grandi) Newsgroups: comp.lang.c++ Subject: Re: porting tips for c++ Message-ID: Date: 10 Aug 90 14:29:35 GMT References: <1990Aug6.133901.25745@swbatl.sbc.com> <943@tcs.tcs.com> <14817@csli.Stanford.EDU> Sender: pcg@aber-cs.UUCP Organization: Coleg Prifysgol Cymru Lines: 48 In-reply-to: keith@csli.Stanford.EDU's message of 8 Aug 90 17:25:15 GMT On 8 Aug 90 17:25:15 GMT, keith@csli.Stanford.EDU (Keith Nishihara) said: keith> [ ... a couple of bad problems with cfront ... ] keith> * Casts are not allowed on lvalues. keith> #define IncPtrBytes(p, inc) ((int)(p) += (inc) // Error. This is not allowed. Technically a cast is (like) an unary operator (in C as in C++, actually in C++ even more), and its result is not an lvalue, it is a value (which means that you can use a cast to force the order of a computation, incidentally). To obtain the effect of an lvalue-cast you can use something like #define IncPtrBytes(p, inc) (*(int *)&(p) += (inc)) // OK except that this does not work (but some compilers allow it!) if 'p' has storage class register. keith> * Furthermore the fix I adopted for the above (using overloaded inline keith> functions) compiled, but appears not to work -- the pointer is never keith> incremented at all: keith> inline void IncPtrBytes(char *p, int inc) keith> { p = (char *)((int)p + inc); } keith> inline void IncPtrBytes(short *p, int inc) keith> { p = (short *)((int)p + inc); } keith> etc. keith> I discovered this during a demo on a customer site, when I no longer keith> had the compiler available to investigate. I may be wrong. You are wrong, I guess. In your example 'p' is passed by value. You should have declared 'p' as a reference to a char pointer (it's not the only problem with your example, but the others are matters of style -- for example, are you sure that pointers on your machine are int when seen as a numeric value?). One could rewrite your example (more portably) as: inline void DoIncPtrBytes(void *ptr,unsigned inc,size_t bytes) { ptr = (char *) ptr + inc*bytes; } #define IncPtrBytes(ptr,inc) DoIncPtrBytes((ptr),(inc),sizeof *(ptr)) There are other (better) variations. -- Piercarlo "Peter" Grandi | ARPA: pcg%uk.ac.aber.cs@nsfnet-relay.ac.uk Dept of CS, UCW Aberystwyth | UUCP: ...!mcsun!ukc!aber-cs!pcg Penglais, Aberystwyth SY23 3BZ, UK | INET: pcg@cs.aber.ac.uk