Fortran90 issue: nonzero out of bounds array elements

In summary, the programmer is having trouble with arrays that have undefined values. He has tried to experiment with changing the matrix's dimensions and has discovered that the values of certain arrays change depending on the dimensions. He is not sure if this is a bug or an intended feature of the code.
  • #1
ilvreth
33
0
Hi to all.

I am programing a new code for some calculation and i have detected a strange issue.
I have my all arrays strictly defined for example

real, dimension(-2:2,-3:2) A, B
complex, dimension(-2:2,1:2) C

In my time dependend calculations i expect the matrix element C(-2,1) to have a maximum value during the whole run, to be approximately about 1.5 and if you attempt the plot t vs C(-2,1) then the curve seems like a Gaussian.


While i use the ifort and gfortran compilers, i attempted an "experiment" to write the matrix element C(-3,1) (which is NOT defined anywhere initialy in my program... because i have complex, dimension(-2:2,1:2) C and not complex, dimension(-3:2,1:2) C)

The i i descovered that if i plot t vs C(-3,1) then the curve i get is a similar to t vs C(-2,1) which is completely strange because is not defined anywhere so i expect to have zero value every time.

I repeat, every array in my program has been defined bounds as i shown above.

How does it happen??
 
Technology news on Phys.org
  • #2
A description of what is happening is nice, but it is better to have a look at your source code. There may be another issue which you have overlooked.

If you wish to post your source code, please use the
Code:
 tags.
 
  • #3
Ok then, I just attempt another experiment which i resize the matrix C from (-2:2,1:2) to (-3:2,1:2) then run the code and you get zero C(-3,2) element. It is about how the memory works i guess. I think this is not a critical issue.
 
  • #4
Yes, it is a memory thing...the truth is that you have no business with memory that you have not defined...for all we know, it is part of some other program running in your computer.

You should probably look into compiling flags to keep an eye on arrays at run time...something like arraybounds or checkarrays or something like that...then, when you run your program and attempt to access an index outside the defined range, the program will stop and let you know.
 
  • #5


Hello,

It seems like you have encountered an issue with array indexing in Fortran90. This language has strict array bounds checking, which means that any attempt to access an array element outside of its defined bounds will result in an error.

In your case, it seems that the compiler is automatically extending the bounds of your arrays to accommodate the out-of-bounds element you are trying to access. This can happen with some compilers, especially when the out-of-bounds element is within a reasonable range from the defined bounds.

To avoid this issue, you can try explicitly defining the bounds of your arrays to include the out-of-bounds element, or you can use the compiler options to disable strict array bounds checking. However, be aware that this can lead to unexpected results and errors in your code.

I would also recommend checking your code for any potential errors in how you are accessing and manipulating your arrays. It is possible that there is a mistake in your code that is causing the strange behavior.

I hope this helps clarify the issue you are experiencing. Best of luck with your program!
 

Related to Fortran90 issue: nonzero out of bounds array elements

1. What is Fortran90 and why is it important?

Fortran90 is a programming language commonly used in scientific and engineering applications. It is important because it allows for efficient and reliable numerical computations.

2. What is meant by "nonzero out of bounds array elements" in Fortran90?

In Fortran90, arrays are a data structure used to store multiple values of the same type. "Nonzero out of bounds array elements" refers to values that are assigned to array elements outside of the declared size of the array.

3. Why is it a problem to have nonzero out of bounds array elements in Fortran90?

Having nonzero out of bounds array elements can lead to unexpected results and errors in the program. It can also cause memory access violations, which can be difficult to debug and can compromise the stability of the program.

4. How can I prevent nonzero out of bounds array elements in my Fortran90 code?

To prevent nonzero out of bounds array elements, it is important to carefully declare the size of the arrays and make sure that all values are assigned within the declared bounds. It is also helpful to use error-checking techniques and debugging tools to catch any potential issues.

5. Are there any specific tips for avoiding nonzero out of bounds array elements in Fortran90?

Yes, some tips for avoiding nonzero out of bounds array elements include using the "DIMENSION" attribute to explicitly declare the size of the array, using the "ALLOCATABLE" attribute to dynamically allocate memory, and using the "DO" loop to iterate through array indices rather than manually assigning values. It is also important to double-check all array assignments and to use compiler warnings to catch any potential issues.

Similar threads

  • Programming and Computer Science
Replies
11
Views
2K
  • Programming and Computer Science
Replies
11
Views
11K
  • Programming and Computer Science
Replies
5
Views
3K
  • Programming and Computer Science
Replies
1
Views
715
  • Programming and Computer Science
Replies
18
Views
6K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
5
Views
3K
  • Programming and Computer Science
Replies
4
Views
15K
  • Programming and Computer Science
Replies
1
Views
3K
  • Programming and Computer Science
Replies
6
Views
3K
Back
Top