Reputation: 4139
Assume that I had an array:
real, dimension(100000, 5) :: a
This array will be filled by real numbers from a(0, :)
till a(n, :)
where n
is a number that lesser than 100000
. After all values are filled then we can decide value of n
(let's assume it 30000
). I want to reshape the array to:
real, dimension(30000, 5) :: a
just to remove unused elements in the array. I don't want any array copy process to be performed because the array is just large, so, it can ruin a program efficiency. Is there any resolution here?
Upvotes: 2
Views: 2257
Reputation: 60008
You can't do anything. There is nothing in the language to shrink an array. Especially not when you declared it as a fixed-size array in the first place. And since you forbid copying, there is really nothing you can do.
You didn't tell us why you want to do that. To save memory? It is a fixed-size array real, dimension(100000, 5)
, it is simply there, you can't save anything. You could use an allocatable array, but for shrinking a copy is necessary.
Or just for easier work with the array? Do it like in the old FORTRAN 77 days, declare a variable n
and use the array always as
a(:n,:)
To make it contiguous? You need to make it allocatable and allocate it to the right size.
To some extent your question is a typical XY problem and you do not tell us what is the actual aim. You are just asking for some technical detail which might not be the best way to solve the final goal. Consider using an allocatable array and allocate it to the right size from the beginning.
Upvotes: 5