1

I'm porting some code from Intel Fortran to gfortran and I can not figure out how to port Intel's INT_PTR_KIND() function.

program main
  integer (INT_PTR_KIND()) v1
  integer v2

  print*, "sizeof v1:", sizeof(v1)
  print*, "sizeof v2:", sizeof(v2)   
  print*, "sizeof INT_PTR_KIND:", INT_PTR_KIND() 
end program main   

gives me with Intel compiler following:

sizeof v1:                     8  
sizeof v2:                     4 
sizeof INT_PTR_KIND:           8

Please, how can I achieve following results in gfortran? I have tried with kind(1), sizeof,...everything gives me 4.

miro
  • 809
  • 10
  • 25

2 Answers2

2

You need to look at the interoperability stuff, use iso_c_binding, in there, there is a special type for C pointers that is possibly what you are looking for.

program test
    use iso_c_binding
    implicit none

    type(c_ptr) :: adr
    integer(8) :: int8
    integer :: int_default

    print*, sizeof(adr)
    print*, sizeof(int8)
    print*, sizeof(int_default)

end program test

follow this link for more.

innoSPG
  • 4,588
  • 1
  • 29
  • 42
  • On my 64arch the output is 8,8,4. I assume that on 32arch the result will be 4,8,4? Correct? – miro Aug 06 '15 at 18:50
  • I think so. For most 32arch, it will be 4, 8 4 as you suggested. But in the general case of 32arch, I have no certitude since some 32 bits system have implemented some trick to support more that 32 bits address, I do not know if it will hold for those systems. – innoSPG Aug 06 '15 at 18:54
2

The correct integer kind from the iso_c_binding module for this is c_intptr_t

i.e. the most direct equivalent of your code is:

use, intrinsic :: iso_c_binding

integer(c_intptr_t) :: v1

integer(8) can mean anything or nothing at all for some compilers.

The integer(c_intptr_t) is directly interoperable with intptr_t from C (size_t vs. uintptr_t).

Community
  • 1
  • 1