Recently I work a lot with Fortran 90 coding and sometimes will get the segmentation errors. This type of error is often related to the management of arrays, because fortran has strict requires to the array size, intialisations, incorrect index etc. Here I summarize the situations that I have met:
1) array initialisation missing
For example, if one uses an array variable without an intialisation.
2) the allocated size and actual size is different
This is often the problem, and one need to spend a lot of time to locate which variable, the allocated size and the actual size
Sometimes if a part of an array is defined and the other part is not given value (only with allocation), then the system may give some huge values to those undefined part in the array, which cause the problem. The safest way is to set the array to, eg., 0.0 if it is real/integer....just after the allocation.
3) Deallocate the array before allocating it, which is always a safe way.
4) If array A has size a1 and array B has size b1,and a1 > b1; if we want to give values of array A by using the array B, one can not simple A(:)= B(:), because they have different size and the compiler may not know how to designate values for indices of A which do not exist in B.
To be safe, we need specify in A which indices are equal to B. For example :
A(:) = 0
A(1:b1) = B(:)
5) Sometimes, even the initial values are set to 0 (for example) for an array A and array B, B has some given values from calculations, there could be errurs if one tries to define the values of A with B, ie., A = B. The undefined values of B (even it is initialized before calculation), supposed to be 0, will become large values in the corresponding position of A. I got this problem when I tried to combine two arrays into one. I have to manually set the value ! Hope to find better way !
In summary, in order to avoid the segmentation errors in fortran 90:
1) Always define the size of array correctly
2) Always initialize the array before getting values from other parts of the program
3) Always give values to the array at the correct corresponding indices
Aucun commentaire:
Enregistrer un commentaire