[petsc-dev] http://www.c-faq.com/aryptr/non0based.html
William Gropp
wgropp at illinois.edu
Tue Mar 30 14:38:02 CDT 2010
You can only add or subtract to addresses in such a way as to remain
within the "allocated block of memory". C itself does *not* assume a
flat address space, so you can't assume that arbitrary address
arithmetic works. But as long as you meet that requirement, you are
ok (i.e., a[-1] is ok if a is a pointer to the inside of an allocated
block of memory such that "a-1" is also a pointer within that block of
memory).
However, as long as the memory is flat, in practice, your allocated
block of memory is all of your memory, so it will work.
Bill
On Mar 30, 2010, at 2:20 PM, Barry Smith 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".
>
> Thanks
>
> Barry
>
>
>>
>> Bill
>>
>> On Mar 30, 2010, at 12:30 PM, Barry Smith wrote:
>>
>>>
>>> What do the PETSc developers think of this? Is it "otherwise,
>>> the behavior is undefined, even if the pointer is not
>>> dereferenced."?
>>>
>>> Whenever we work with global indices like in
>>> FormFunctionLocal() we do this.
>>>
>>> Is PETSc wrong to ever do this? Is there any other way to
>>> allow users to work with global indices in a simple way? Do we
>>> need to strip out all the FormFunctionLocals() from PETSc? Not the
>>> end of the world but it provides such a nice simple interface for
>>> simple problems it seems a pity to toss it.
>>>
>>> Thanks
>>>
>>> Barry
>>>
>>
>> William Gropp
>> Deputy Director for Research
>> Institute for Advanced Computing Applications and Technologies
>> Paul and Cynthia Saylor Professor of Computer Science
>> University of Illinois Urbana-Champaign
>>
>>
>>
>>
>
William Gropp
Deputy Director for Research
Institute for Advanced Computing Applications and Technologies
Paul and Cynthia Saylor Professor of Computer Science
University of Illinois Urbana-Champaign
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-dev/attachments/20100330/53e5f428/attachment.html>
More information about the petsc-dev
mailing list