Increasing Size of FORTRAN Array without Reallocation

Click For Summary
SUMMARY

In FORTRAN, it is not possible to directly increase the size of an already allocated array without reallocation. The common approach involves creating a temporary array to hold the existing values, deallocating the original array, and then allocating a new array with the desired size. For example, to expand an array from size 3 to size 4, one would allocate a temporary array, copy the values, deallocate the original, and then allocate the new array with the additional element. This method ensures data integrity while resizing the array.

PREREQUISITES
  • Understanding of FORTRAN array allocation and deallocation
  • Familiarity with FORTRAN syntax for array manipulation
  • Knowledge of memory management in programming
  • Basic concepts of dynamic memory allocation
NEXT STEPS
  • Research FORTRAN array handling techniques
  • Learn about dynamic memory allocation in FORTRAN
  • Explore alternatives to reallocating arrays in FORTRAN
  • Investigate best practices for memory management in FORTRAN
USEFUL FOR

This discussion is beneficial for FORTRAN programmers, software developers working with numerical computing, and anyone involved in memory management and optimization in FORTRAN applications.

Zahur
Messages
1
Reaction score
0
Is it possible to increase the size of an already allocated array in FORTRAN, without reallocation?
e.g. if in start A(3)=[4, 5, 6] and now I want something like A(4)=[4, 5, 6, 7].
Currently I am using a temporary array to do this

allocate(A(3))
A=(/4, 5, 6/)
allocate(temp(3))
temp=A
deallocate(A)
allocate(A(4))
A(1:3)=temp
A(4)=7


Or some other suggestion

Zahur
 
Technology news on Phys.org
I'm not a Fortran programmer, but some googling lead me to this. NB: I've not read the code, or tested it.
 

Similar threads

  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 5 ·
Replies
5
Views
8K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
921
  • · Replies 6 ·
Replies
6
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
13K
  • · Replies 4 ·
Replies
4
Views
12K
  • · Replies 23 ·
Replies
23
Views
2K