1

In Fortran, you can reshape arrays with pointers:

program working

  implicit none

  integer, dimension(:,:), pointer :: ptr
  integer, dimension(6), target :: trg

  trg = (/1,2,3,4,5,6/)
  ptr(1:2,1:3) => trg

  ! Here, you can use the 6-sized 1D array trg
  ! or the 2 by 3-sized 2D array ptr at the same time.
  ! Both have the same content (6 integers), but are only indexed
  ! differently.

  write(*,*) ptr(1,2)

end program working

This program writes "3", which is according to the reshape rules.

Similarly, I attempted to do the same, but not with 1D to 2D, but 0D to 1D.

program not_working

  implicit none

  integer, dimension(:), pointer :: ptr
  integer, target :: trg

  trg = 1
  ptr(1:1) => trg

  ! I hoped to be able to use the scalar trg at the same time as
  ! the one-sized 1D array ptr. I thought they both have the same
  ! content, but are only indexed differently.

  write(*,*) ptr(1)

end program not_working

I expected to see a "1". But it does not compile.

Gfortran 4.9 says:

Error: Rank remapping target must be rank 1 or simply contiguous at (1)

Ifort 14.0.2 says:

<file>.f90: catastrophic error: Internal compiler error: segmentation violation signal raised Please report this error along with the circumstances in which it occurred in a Software Problem Report. Note: File and line given may not be explicit cause of this error. compilation aborted for <file>.f90 (code 1)

I do not understand how the scalar trg can be not contiguous and what the fundamental difference between the two example programs is.

3
  • Use tag fortran and add another tag for a specific version when necessary (not here). You are actually using a Fortran 2003 feature. Commented May 25, 2016 at 9:04
  • Internal compiler error is always an error in the compiler. It may be caused by an invalid code, but always it is a bug in the compiler. A compiler should not crash even if you supply wrong code. It also clearly says what you should do "Please report this error along with the circumstances in which it occurred in a Software Problem Report. " Have you done it? Commented May 25, 2016 at 9:06
  • Note that Intel version 14 is a bit old and you should think about an upgrade. Commented May 25, 2016 at 9:07

2 Answers 2

0

The scalar is not simply contiguous array because it is not an array at all. It is as simple as that. Gfortran detects it and complains, ifort is confused and crashes. But your code is invalid, you cannot point an array pointer on a scalar.

Sign up to request clarification or add additional context in comments.

Comments

0

Array pointers are desinged to point to arrays, so cannot point to scalars (instead, we can use scalar pointers for this purpose). But if we definitely want to use array pointers to point to scalars (for some reason), we could use c_f_pointer() such that

use iso_c_binding

integer, target :: a
integer, pointer :: p(:), q(:,:), r(:,:,:)

a = 777

call c_f_pointer( c_loc( a ), p, [1] )
call c_f_pointer( c_loc( a ), q, [1,1] )
call c_f_pointer( c_loc( a ), r, [1,1,1] )

print *, "p = ", p(:),     "with shape", shape(p)
print *, "q = ", q(:,:),   "with shape", shape(q)
print *, "r = ", r(:,:,:), "with shape", shape(r)

But this is clearly an "unsafe" feature (in the sense that it allows access to raw memory), and if used with wrong arguments, it could give a wrong result (or even disaster), for example:

call c_f_pointer( c_loc( a ), p, [3] )

print *, "p = ", p(:)   !! gives "p =  777  202 0" etc with garbage data

So, unless there is some special reason, I think it is probably better (safer) to use scalar pointers for scalar variables...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.