Unusual segmentation fault with RESHAPE

This is the sample code that i’m trying to map from 3D to 1D.

PROGRAM test
  INTEGER, PARAMETER :: dp = SELECTED_REAL_KIND(P=13, r=200)
  REAL(KIND=dp), DIMENSION(608, 64, 64) :: tmp_ca1, tmp_Ca2
  REAL(KIND=dp) :: ca1, Ca2
  REAL(KIND=dp), DIMENSION(608*64*64) :: sca1, sca2
  INTEGER :: stencil_radius = 4
  INTEGER :: maxx = 608, maxy = 64, maxz = 64
  
  ca1 = 0.1d0
  Ca2 = 1d3
  
  tmp_ca1(STENCIL_RADIUS+1:MAXX+STENCIL_RADIUS, STENCIL_RADIUS+1:MAXY+STENCIL_RADIUS, &
        STENCIL_RADIUS+1:MAXZ+STENCIL_RADIUS) = ca1

  tmp_Ca2(STENCIL_RADIUS+1:MAXX+STENCIL_RADIUS, STENCIL_RADIUS+1:MAXY+STENCIL_RADIUS, &
        STENCIL_RADIUS+1:MAXZ+STENCIL_RADIUS) = Ca2

  sca1 = RESHAPE(tmp_ca1, (/ 608*64*64 /) )
  sca2 = RESHAPE(tmp_Ca2, (/ 608*64*64 /) )

END PROGRAM test

I’m using FOrtran 10.9.

Tuan

Hi Tuan,

The reshape is fine. You have an out of bounds error when initializing tmp_cal.

% pgf90 reshape.f90 -V10.9 -g -Mbounds
% a.out
0: Subscript out of range for array tmp_ca1 (reshape.f90: 12)
    subscript=609, lower bound=1, upper bound=608, dimension=1

Hope this helps and Go Ducks,
Mat