From: Timo Felbinger <timof@uranos.quantum.physik.uni-potsdam.de>
Subject: Re: A Problem of Porting Programs from Sun Solaris 2.x to OSF1
Date: 1998/10/21 [thread overview]
Message-ID: <Pine.LNX.3.96.981021130334.21032E-100000@uranos.quantum.physik.uni-potsdam.de> (raw)
In-Reply-To: 362DAF40.CEA2D66C@nospam.euronet.nl
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 <stddef.h>), 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
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 ` Niel Markwick
1998-10-21 4:00 ` Niel Markwick
1998-10-21 4:00 ` Timo Felbinger [this message]
1998-10-21 4:00 ` Niel Markwick
1998-10-19 4:00 ` Finnbarr P. Murphy
-- 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