Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Stack overflow and variable declaration in the subroutine 1

Status
Not open for further replies.

venexiano

Programmer
Mar 12, 2009
3
IT
Hi to everybody. I have a question about stack overflow. Im vorking with Compaq Visual fortran. I'm using fortran 77. when the run of my code get to this subroutine:

SUBROUTINE reconstructionWcar
!
implicit none
INCLUDE 'PRICE2D.DIM'
!
integer i,j,jj,K,L,N,ste,M,Nok
REAL*8 Ax(MAXVAR,MAXVAR),Ay(MAXVAR,MAXVAR),An(MAXVAR,MAXVAR,3)
REAL*8 EIG(MAXVAR,MAXVAR,3),INV(MAXVAR,MAXVAR),OIokENO(0:3),
& NOTO(ne),Akl(ne,LL),DOG(LL,MAXVAR,7),CSmed(MAXVAR),
& EIGinv(MAXVAR,MAXVAR),BUTTA(MAXVAR),DOGcar(MAXVAR,LL,7,0:3),
& DOGENO(MAXVAR,7,0:3),OIweno(7),OIwenoRIGHE,SUMalphaW,
& alphaW(7),OIok(7),DOGcarWENO(MAXVAR,LL),omegaW(7),OIeno(0:3)
& ,OIenoRIGHE,MAXIM ,
& DOGok(MAXVAR,LL,MAXELE)

RETURN
END

MAXVAR is a parameter(=10000), LL and Ne are 2 common variables,all of them declared in the included 'PRICE2D.DIM' file, as well the COMMON declaration are all in that file. I deleted everything, i left just the declaration and it gives me a stack overflow error when it arrives at the last line of declaration( variable DOGok(MAXVAR,LL,MAXELE)). if i cancel the variable DOGok it works. In Project -> Settings => Link -> Project Options I tried to set /stack:2000000 or even up to /stack:2000000000 but i still have the error. So at the end i thought i have really the maximum allowed stack . But at the end i tried to declare DOGok(MAXVAR,MAXLL,MAXELE) instead of DOGok(MAXVAR,LL,MAXELE) where MAXLL is a parameter(=40) and i got rid of the error! i don't understand why!!!!! in fact LL in my particular runs is 3!!!!and at maximum it can be 40, but now it is just three!and when debugging i checked his value out and it is 3!!!! why with a parameter = 40 works and with a variable =3 does not?
thanks so muck
venexiano
 
Where is LL set? Is it a parameter (as in Fortran 77 parameter types) or something in a common block?

If it is set in a common block, then the value is unknown at build time, even though it may be assigned when yout call the routine. They way it computes the index of multi dimensional arrays is by multiplying by the maximum sizes of the dimensions. Eg if an array is declared as (3, 4, 6), to get to (2, 1, 3) the computation is (((3 -1)*6) + 1 - 1) * 4 + 2 - 1. If it doesn't know the 4 or the 6 are the 2nd and 3rd dimensions, it cannot do this computation. If any one of them is unset, it could be a very big number, eg 3435973836 (Hex CCCCCCCC), which when multiplied out, gives you an enormous index which then blows your data segment or stack.
 
thanks a lot for your answer. how i wrote in my previous message MAXVAR is a parameter(=10000), LL and Ne are 2 common variables(defined in a common block that is written in the included 'PRICE2D.DIM' file). i dont understand 2 things:

1) In the case it does not know the second dimension of OGok(MAXVAR,LL,MAXELE) how does it allocates the matrix? Or better: LL is equal to 3 when i run the code and i debug the code, so why when it runs to the SUBROUTINE reconstructionWcar and it starts to allocate it does not use the value LL = 3? if it does not use 3, which value does it use?

2)it s not clear to me your computation... in your example the component (2, 1, 3) of array (3, 4, 6) is at the 26th position (fortran allocate by columns) but (((3 -1)*6) + 1 - 1) * 4 + 2 - 1 is not equal to 26... am i wrong somewhere?

thanks a lot
venexiano
 
1) I don't really know how the matrices are allocated. F77 did not have the concept of dynamic arrays.

2) Sorry, I got it slightly wrong - the last -1 shouldn't be there. I was in C mode where arrays begin at 0. In Fortran, arrays begin at 1 so the last -1 shouldn't be there.

(((3 -1)*6) + 1 - 1) * 4 + 2
= 12*4 +2
= 24 + 2
= 26

The only thing I can suggest is that if you want to use variable size 3d arrays, is to fix the dimensions of the array.
 
Thanks... but still the computation is wrong since 12*4=48 not 24. and so:

(((3 -1)*6) + 1 - 1) * 4 + 2
= 12*4 +2
= 48 + 2
= 50

so it s not clear to me which is the rule to get the position using (3, 4, 6).
thanks
venexiano
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top