GianLuigi Piacentini
2023-11-15 18:40:00 UTC
Hi all,
I have a perhaps silly question, that puzzles me before committing to
write actual code.
Please consider a data structure which is basically an array of arrays
type element
integer, allocatable :: element_core(:)
end type
...
type(elements), allocatable :: array_of_elements(:)
...
allocate ( array_of_elements(some_size) )
(this is not a matrix, each element may significantly differ in size,
and someone may be long (and subjected to reallocation cycles, but his
is plain reallocation).
Now I have to increase size of previously allocated array_of_elements,
using the usual pattern (at least I think it's usual)
type(elements), allocatable :: tmp(:)
allocate ( tmp(new_size) )
tmp(1:some_size) = array_of_elements ! ***
call move_alloc(from = tmp, to = array_of_elements)
Seems to me that during the operation marked with the *** comment the
various elements are also copied, and this seems vasteful.
Is there a way to avoid such copying, if it really happens ?
Perhaps making array_of_elements an array of pointers to allocated
elements ?
Thanks in advance
Gigi Piacentini
I have a perhaps silly question, that puzzles me before committing to
write actual code.
Please consider a data structure which is basically an array of arrays
type element
integer, allocatable :: element_core(:)
end type
...
type(elements), allocatable :: array_of_elements(:)
...
allocate ( array_of_elements(some_size) )
(this is not a matrix, each element may significantly differ in size,
and someone may be long (and subjected to reallocation cycles, but his
is plain reallocation).
Now I have to increase size of previously allocated array_of_elements,
using the usual pattern (at least I think it's usual)
type(elements), allocatable :: tmp(:)
allocate ( tmp(new_size) )
tmp(1:some_size) = array_of_elements ! ***
call move_alloc(from = tmp, to = array_of_elements)
Seems to me that during the operation marked with the *** comment the
various elements are also copied, and this seems vasteful.
Is there a way to avoid such copying, if it really happens ?
Perhaps making array_of_elements an array of pointers to allocated
elements ?
Thanks in advance
Gigi Piacentini