Software Archive
Read-only legacy content
17060 Discussions

Expandable array, pointers and stack overflow

fela
Beginner
530 Views
In the following programme i try to make an "expandable array". When MXOSIZ=12 the line CXDATA(1:MXOSIZ)=CXTEMP causes a stack overflow. Why do I get a stack overflow? Can I structure my programme differently in order not to get a stack overflow? And is there a better way to make an "expandable" array.

PROGRAM GROW     
     
IMPLICIT NONE      
     
!--------------------------------------------------------------   
!  **** PARAMETERS ****     
!--------------------------------------------------------------      
INTEGER(4),      PARAMETER :: MXDATL=100000      
INTEGER(4),      PARAMETER :: MXGROW=2     
     
!--------------------------------------------------------------  
!  **** VARIABLES ****     
!--------------------------------------------------------------      
INTEGER(4)                 :: MXALLO     
INTEGER(4)                 :: MXOSIZ     
INTEGER(4)                 :: MXSTAT     
INTEGER(4)                 :: MXLOOP     
INTEGER(4)                 :: MXNREC     
INTEGER(4)                 :: MXSIZE     
INTEGER(4)                 :: MXINDX     
     
CHARACTER(MXDATL), POINTER :: CXTEMP(:)     
CHARACTER(MXDATL), POINTER :: CXDATA(:)     
     
!--------------------------------------------------------------   
!  INITIALIZE.       
!--------------------------------------------------------------   
     
MXNREC=0     
MXSIZE=0     
     
!--------------------------------------------------------------   
!  FILL DATA IN ARRAY.     
!--------------------------------------------------------------   
     
DO MXLOOP=1,100     
     
!--------------------------------------------------------------   
!  INCREASE THE NUMBER OF RECORDS BY ONE.     
!--------------------------------------------------------------   
    
MXNREC=MXNREC+1     
     
!--------------------------------------------------------------   
!  ALLOCATE MORE SPACE IF OUT OF SPACE.     
!--------------------------------------------------------------     
IF (MXNREC.GT.MXSIZE) THEN     
     
IF (MXSIZE.GT.0) THEN     
CXTEMP=>CXDATA     
PRINT*,SIZEOF(CXTEMP)      
ENDIF     
     
MXOSIZ=MXSIZE     
MXSIZE=MXSIZE+MXGROW     
     
ALLOCATE(CXDATA(MXSIZE),STAT=MXALLO)     
     
IF (MXOSIZ.GT.0) THEN     
CXDATA(1:MXOSIZ)=CXTEMP   !STACK OVERFLOW WHEN MXOSIZ=12      
DEALLOCATE(CXTEMP,STAT=MXALLO)     
ENDIF     
     
ENDIF     
     
!--------------------------------------------------------------    
!  SAVE THE RECORD.     
!--------------------------------------------------------------    
     
CXDATA(MXNREC)='12345'     
     
ENDDO      
     
END
0 Kudos
1 Reply
Steven_L_Intel1
Employee
530 Views
It would appear that the compiler is creating a stack temporary as part of the assignment, and it is exceeding the default 1MB stack allocation. I'm not sure why a temporary is used here - I do know that our removal of unnecessary temps isn't done as much for CHARACTER types as it is for numerics.

I'll pass this on to our experts in this area for their consideration.

Steve
0 Kudos
Reply