How to get the address of an array pointer?

How to get the address of an array pointer?

Dear all,

I would like to get the address of an array pointer. The prototype codes are as following:

program main

 

    implicit none

 

    type foo

        integer, allocatable :: i(:)

        integer j

    end type

 

    type(foo) a

 

    print *, "The address of a is ", loc(a)

    print *, "The address of a%i is", loc(a%i) ! I expect the same address as a.

 

end program main

My final target is to get the address of "a" with type(foo) through the address of "a%i", since "i" is the first part of type(foo).

Thanks in advance!

Li

 

5 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

As explained elsewhere, the descriptor for the allocatable component a%i might (there is no guarantee) be the first thing in the storage for foo, but the actual data for a%i will be somewhere completely different. loc(a%i) and loc(a) will be unrelated.

Quote:

IanH wrote:

As explained elsewhere, the descriptor for the allocatable component a%i might (there is no guarantee) be the first thing in the storage for foo, but the actual data for a%i will be somewhere completely different. loc(a%i) and loc(a) will be unrelated.

So if I want to associate the address of "loc(a%i)" with "a", I can only use hash table?

(Fascinating - quoting in a response reveals the quotee's email address. Well, at least I won't have to struggle to remember my login id from now on.)

Noting that I'm not even vaguely familiar with your requirements - doing a reverse lookup on the pointer is one possibility, but before doing that I'd have a good think about what you are trying to do.

Quote:

Li Dong wrote:

Dear all,

I would like to get the address of an array pointer. The prototype codes are as following:

program main

    implicit none

    type foo

        integer, allocatable :: i(:)

        integer j

    end type

    type(foo) a

    print *, "The address of a is ", loc(a)

    print *, "The address of a%i is", loc(a%i) ! I expect the same address as a.

end program main

My final target is to get the address of "a" with type(foo) through the address of "a%i", since "i" is the first part of type(foo).

Thanks in advance!

Li

loc(a) is the address of type foo variable a (think of C++ struct or class). Within variable a (struct foo a) is an array descriptor named "i" (think of this as C++ vector < int > i) where: loc(a%i) is same/equivilent to loc(a%i(1)), meaning the loc of an array is the location of the first element of the array and not the address of the array descriptor. This is unlike C++ where address of i is the address of the vector and not the address of i[0]. i being allocatable, places the (assumed) allocated array data outside the bounds of the type foo object. It would be easier to associate loc(j) with the array i. Jim Dempsey

Leave a Comment

Please sign in to add a comment. Not a member? Join today