I am trying to use an array that has been dynamically allocated, and although the compiler has no problem, during run time I get a segmentation fault error. I realize this is usually the error one receives when one is trying to access memory outside of what has been allocated, but I am not doing this. Can you find fault with the technique I am using here?
Here is my makefile:
PROGRAM = nautreverb FC = /opt/pgi/linux86-64/6.1/bin/pgf77 FSRCS = nautreverb.f FOBJS = $(FSRCS:.f=.o) INCLUDE = -I. -L. FFLAGS = -g $(PROGRAM): $(FOBJS) $(FC) $(FFLAGS) -o $@ $(INCLUDE) $(FOBJS) clean: rm *.o
And here is my code:
program nautreverb implicit none real*8 disttp(*) integer nn, nxtot, nytot, nx, ny integer*4 malloc integer*4 dsize pointer( disttp_ptr,disttp) dsize = 8 nxtot = 10 nytot = 10 disttp_ptr = malloc(dsize*1001*1001) nn=0 do 100 nx=1,nxtot do 200 ny=1,nytot nn=nn+1 write(*,*) nx,ny,nn disttp(nn) = 1 write(*,*) disttp(nn) 200 continue 100 continue end
The output is
1 1 1
Note that if I change the allocated arraysize to be dsize101101, the code works fine with no seg fault. This is also the case if I simply declare the array to be 1001*1001 instead of allocating. What is the problem?