From: Niel Markwick <nielm@nospam.euronet.nl>
To: Yingwei Peng <ypeng@math.mun.ca>
Subject: Re: A Problem of Porting Programs from Sun Solaris 2.x to OSF1
Date: 1998/10/21 [thread overview]
Message-ID: <362DAF40.CEA2D66C@nospam.euronet.nl> (raw)
In-Reply-To: 362B3237.7E35E05@euronet.nl
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!)
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.
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 <stddef.h>), and the calculation of the value of
'i' should somewhere involve 'sizeof(double)' to get the offset right!
Remember, you can always exclude a bit of SOLARIS code and replace it
with a DEC Unix hack with:
#if (defined(__osf__) && defined(__alpha))
/* DEC Unix version of code */
#else
/* other platforms */
#endif
but I always feel that this is a last resort - in case someone else
wants to do further porting (perhaps to a 128 bit platform!)
Good luck,
Niel.
PS.
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!
--
Niel Markwick (remove nospam. to reply to my email address)
Consultant Software Engineer
Kontich, Belgium.
next prev parent reply other threads:[~1998-10-21 4:00 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
1998-10-18 4:00 A Problem of Porting Programs from Sun Solaris 2.x to OSF1 Yingwei Peng
1998-10-19 4:00 ` Finnbarr P. Murphy
1998-10-19 4:00 ` Niel Markwick
1998-10-21 4:00 ` Niel Markwick [this message]
1998-10-21 4:00 ` Timo Felbinger
1998-10-21 4:00 ` Niel Markwick
-- strict thread matches above, loose matches on Subject: below --
1998-10-24 4:00 Yingwei Peng
read-only archive
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox