Home > Archive > Fortran > November 2005 > associated pointer on 0 size vector
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
associated pointer on 0 size vector
|
|
| Umberto D'Ortona 2005-11-22, 7:01 pm |
| Hello,
I have this little problem :
In a program there is plenty of matrices, and there is a vector of
matrix pointers pointing on these matrices. unfortunately, some of these
matrices have zero size. When I perform some operation on all matrices,
I need to see if a pointer (pointing on the zero size matrix) is
associated with the (associated) matrix. And the answer depends on the
compiler. Here is a (extremly simplified) example :
program testpointer
integer, dimension(:), allocatable, target :: vec
integer, dimension(:), pointer :: pvec
n=1 ; m=0
allocate(vec(n:m)) ! zero size vector
pvec=>vec ! pointer on zero size vector
print*,associated(pvec,vec)
end program testpointer
I obtain TRUE for Ibm xlf, and SGI f90
and FALSE on Intel ifort, g95 and Lahey lf95 (all on linux)
Should the answer be TRUE, FALSE or this is not define ?
thank you
umberto
| |
| Michael Metcalf 2005-11-22, 7:01 pm |
|
>
> I obtain TRUE for Ibm xlf, and SGI f90
> and FALSE on Intel ifort, g95 and Lahey lf95 (all on linux)
>
> Should the answer be TRUE, FALSE or this is not define ?
>
It is defined, to be false (see also "Fortran 95/2003 Explained", Section
8.2).
Regards,
Mike Metcalf
| |
| Richard E Maine 2005-11-22, 7:01 pm |
| Michael Metcalf <michaelmetcalf@compuserve.com> wrote:
> It is defined, to be false (see also "Fortran 95/2003 Explained", Section
> 8.2).
I had to go check because I thought I recalled the opposite answer, but
Mike is right here. I personally regard the definition of associated for
zero-sized objects to be a mess. It is special-cased in such a way that
it never does anything "useful". Basically, two zero-sized things are
never associated with each other. As far as I can tell, this even
applies to the extreme of asking whether a zero-sized pointer is
associated with itself; the answer is no. At one time it was proposed
that the answer be the opposite, that two zero-sized objects of the same
kind and rank were always associated. I didn't like that answer much
better, and I have trouble remembering which of the two nonsense (in my
opinion) answers ended up passing. I have to look it up or I get it
wrong.
I'd have preferred a different treatment, where the system actually can
keep track of whether the two objects were the result of the same
allocation or not. Unfortunately, there are cases where that isn't
feasible. I don't feel like spending the time to reconstruct those
cases; I sort of recall the general context, but I would have to
reconstruct details, and it isn't worth the time because it is now just
past history and not readily changeable. I didn't like this
interpretation at the time and I still don't, but that's the way it is,
I'm afraid.
So the result is that the standard does specify the result, but I find
that it is specified in such a way as to basically be useless for
zero-sized objects in my opinion.
--
Richard Maine | Good judgment comes from experience;
email: my first.last at org.domain| experience comes from bad judgment.
org: nasa, domain: gov | -- Mark Twain
| |
| Umberto D'Ortona 2005-11-23, 3:57 am |
|
Thanks for the answer,
I would have prefered the answer to be true, since zero size matrices
will need a special treatment in all the code.
I'll have to deal with it.
umberto
| |
| Joe Krahn 2005-11-23, 9:57 pm |
| Richard E Maine wrote:
> Michael Metcalf <michaelmetcalf@compuserve.com> wrote:
>
>
>
>
> I had to go check because I thought I recalled the opposite answer, but
> Mike is right here. I personally regard the definition of associated for
> zero-sized objects to be a mess. It is special-cased in such a way that
> it never does anything "useful". Basically, two zero-sized things are
> never associated with each other. As far as I can tell, this even
> applies to the extreme of asking whether a zero-sized pointer is
> associated with itself; the answer is no. At one time it was proposed
> that the answer be the opposite, that two zero-sized objects of the same
> kind and rank were always associated. I didn't like that answer much
> better, and I have trouble remembering which of the two nonsense (in my
> opinion) answers ended up passing. I have to look it up or I get it
> wrong.
>
> I'd have preferred a different treatment, where the system actually can
> keep track of whether the two objects were the result of the same
> allocation or not. Unfortunately, there are cases where that isn't
> feasible. I don't feel like spending the time to reconstruct those
> cases; I sort of recall the general context, but I would have to
> reconstruct details, and it isn't worth the time because it is now just
> past history and not readily changeable. I didn't like this
> interpretation at the time and I still don't, but that's the way it is,
> I'm afraid.
>
> So the result is that the standard does specify the result, but I find
> that it is specified in such a way as to basically be useless for
> zero-sized objects in my opinion.
>
That is amazingly senseless. I would understand this if one was not
allow to have zero-sized allocations, at least for pointers. But, the
allocated array and pointer both claim to be allocated/associated, but
not with each other.
I assume the issue has to do with not wanting to actually allocate
memory for zero-sized. So, the actual memory pointers cannot have the
same value. But, this could be worked around by having the internal data
pointers associated with an allocated zero-size array point directly to
the allocatable structure.
Surely, there must be a sensible work-around for what must be an
implementation difficulty.
It is now well established for F9x, but it is a standard that is
apparently not well adhered to. So, maybe there is an epsilon chance of
getting the Standards people to change this for the next Fortran spec??
Or, if there are strong feelings one way or the other, maybe it could be
at least be officially allowed to be a compiler option?
Joe Krahn
| |
| Richard Maine 2005-11-23, 9:57 pm |
| Joe Krahn <krahn@niehs.nih.gov> wrote:
> That is amazingly senseless.
[the special-casing of the zero-size association rules]
I agree.
> I assume the issue has to do with not wanting to actually allocate
> memory for zero-sized.
Yes, but "not wanting to" is an understatement. Remember that pointers
can get associated in ways other than by doing an allocation. They can,
for example, point at slices (perhaps zero sized) of existing data. In
that case, there isn't any allocation going on at all.
If I recall correctly, COMMON and equivalence are the things that really
mess it up. I'd have been happy with forbidding things from working with
common, but that wasn't going to fly (or come close) with a majority.
> It is now well established for F9x, but it is a standard that is
> apparently not well adhered to. So, maybe there is an epsilon chance of
> getting the Standards people to change this for the next Fortran spec??
Maybe. I'd bet against it, but you are free to try. I won't actively
work against it. But I won't volunteer to champion it either. I tried to
fight this long ago. Lost that fight and I'm not willing to take on the
same battle again. It isn't as though the problem actually comes up a
lot.
> Or, if there are strong feelings one way or the other, maybe it could be
> at least be officially allowed to be a compiler option?
There is no "official" allowance needed. Compilers could have a switch
for that today. There are lots of cases where compilers have switches
that make them non-conforming with one switch setting. All it would take
would be enough user demand. My guess would be that this doesn't
actually come up enough for enough demand to be there, but that's just
my guess.
--
Richard Maine | Good judgement comes from experience;
email: last name at domain . net | experience comes from bad judgement.
domain: summertriangle | -- Mark Twain
| |
| Dick Hendrickson 2005-11-24, 9:56 pm |
|
Joe Krahn wrote:
> That is amazingly senseless. I would understand this if one was not
> allow to have zero-sized allocations, at least for pointers. But, the
> allocated array and pointer both claim to be allocated/associated, but
> not with each other.
>
Well, not completely senseless. The odd case is for
pointers that point to slices that are zero sized.
Given something like
P => A(N:M)
the current rules say ASSOCIATED(P, A(N+1:M+1)) will always
be false. It seems equally strange to me to say that the
result depends on the values of N and M.
The current rules are senseless, but no more so than the
other options.
Dick Hendrickson
|
|
|
|
|