From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fee84,694e2bdb09c0ca09 X-Google-Attributes: gidfee84,public From: Timo Felbinger Subject: Re: A Problem of Porting Programs from Sun Solaris 2.x to OSF1 Date: 1998/10/21 Message-ID: #1/1 X-Deja-AN: 403518706 References: <362A4479.1BFE88FC@math.mun.ca> <362B3237.7E35E05@euronet.nl> <362DAF40.CEA2D66C@nospam.euronet.nl> Content-Type: TEXT/PLAIN; charset=US-ASCII Organization: University of Potsdam Mime-Version: 1.0 Reply-To: Timo.Felbinger@quantum.physik.uni-potsdam.de Newsgroups: comp.unix.osf.osf1 On Wed, 21 Oct 1998, Niel Markwick wrote: > Yingwei Peng wrote by email: > > > > Thank you for telling me this web site. I just read it briefly > > and it seemed that I have a lot of work to do. For example, if my > > understanding is correct, var2 = var1 + i is no longer valid, where > > var1 and var2 are two pinters of double and i is an integer, because > > of 64bit pointers in DEC!? I will check this document carefully > > BTW there was no warning message when I compiled my codes. > > > > It is not 'invalid' as such, it just may cause problems if 'i' and var2 > are not defined portably (ie involving sizeof(double)) > > However, there are more clean and portable ways of doing this... eg if > var1 is pointer to an array of double's > var2 wants to be pointer to 'n'th double in the array > then > double *var2 = &var1[n]; > will work on any platform with no problems (and is a bit more readable > IMHO!) In C, both var1[n] and *(var1+n) have the same effect; when adding an integer to a pointer, the size of what this pointer points to will be taken into account automatically. What's more, you can even write n[var1] this, too, is equivalent to *(var1+n). In other words, if both var1 and var2 are arrays of double, you may write 7[var2] = 4[var1] to copy the value of the 4th element of var1 into the 7th element of var2. Yes, I know this is ugly and I didn't believe it when I saw it the first time, but nothing in the language standard forbids it, and at least some compilers will actually accept this (I tested this with gcc 2.8.1). Of course, &var1[n] may well be the most readable way to code. > Also, remember that if var1 is a pointer returned from malloc, where the > size has been non-portably defined (eg "16" instead of > "4*sizeof(double)" for a 4 - double array), then there may not be enough > valid memory for the required storage, so var2 will point to invalid > memory. True. > Finally, If you are going to stick with the original code, 'i' should > not be an integer, but size_t (a portable scalar type used for pointer > arithmetic, defined in ), and the calculation of the value of > 'i' should somewhere involve 'sizeof(double)' to get the offset right! Using size_t instead of int is a good idea, but you will certainly not have to involve sizeof(double) to get the correct offset for a pointer into an array of double! > As a humorous aside, in the past, I have been chastised for using > sizeof(...) in my code - the pointy haired boss in question told me that > calling sizeof so may times was a performance issue, and he wanted me to > do something like: > #define SIZEOF_POINTER 8 > in to avoid the extra function calls! > > It took a LOT of persuasion to convince him that it wasn't really a > function call, and the compiler simply replaced sizeof(blah) with a > scalar during compilation! Could have been worse. A really pointy haired boss would have asked you to do it in a more portable way (avoiding the hard-coded constant 8): int sizeof_pointer; main() { sizeof_pointer = sizeof(int *); . . . } and use the global variable whenever you need the size of a pointer ;-) Timo Felbinger (Timo.Felbinger@quantum.physik.uni-potsdam.de) Quantum Physics Group Institute of Physics University of Potsdam, Germany