How to get the address of an array pointer?

How to get the address of an array pointer?

Imagen de Li Dong

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

Do few things, and do them well!
publicaciones de 5 / 0 nuevos
Último envío
Para obtener más información sobre las optimizaciones del compilador, consulte el aviso sobre la optimización.
Imagen de IanH

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.

Imagen de Li Dong

Cita:

Ian.Harvey 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?

Do few things, and do them well!
Imagen de IanH

(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.

Imagen de jimdempseyatthecove

Cita:

dong_li_1985 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

www.quickthreadprogramming.com

Inicie sesión para dejar un comentario.