[petsc-dev] http://www.c-faq.com/aryptr/non0based.html
Jed Brown
jed at 59A2.org
Tue Mar 30 19:39:15 CDT 2010
On Tue, 30 Mar 2010 14:20:01 -0500, Barry Smith <bsmith at mcs.anl.gov> wrote:
>
> On Mar 30, 2010, at 12:55 PM, William Gropp wrote:
>
> > As long as you have a flat address space, this trick works. The
> > problem is (was) for systems with memory segments; in that case,
> > &realarray[-1] might not be valid.
>
> Bill,
>
> So is it then a question of using &something[invalidindex] that
> is the only problem? We never do this in PETSc; we only do, for
> example, a -= mstart; that is add or subtract to an already existing
> valid address. Of course a[0] may now be invalid, but then the code is
> written so those invalid indices are never used.
>
> In other words replace
>
> int *array = &realarray[-1];
> with
> int *array = &realarray[0];
> array--; now use array[1]
> Is the second form any more "valid" or "legal" then the first form? Or
> are they equally "wrong".
These are equivalent(ly wrong) because a[i] == *(a+i) == i[a].
I've never run code on a platform with segmented memory, are there any
PETSc users that do? I would expect you'd have seen petsc-maints about
this if there were.
Jed
More information about the petsc-dev
mailing list