Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

A (silly) question on allocate-on-assignment and sourced allocation

91 views
Skip to first unread message

GianLuigi Piacentini

unread,
Feb 20, 2021, 4:52:19 AM2/20/21
to
Hi all,

when "assigning" something a to a class(*) allocatable, I used to write

class(*), allocatable :: lhs

type(something) :: rhs

(think rhs as to a complicated derived type with allocatables and
pointer components, some allocated/associated and some not, or to
another class(*) variable)

allocate (lhs, source = rhs)

now (gfortran 10) I discovered I could simply write

lhs = rhs

and it works (at least with the simple example I so far tested).

What's the difference between the 2 "assignments" ?

Thanks in advance
Gigi

spectrum

unread,
Feb 22, 2021, 12:48:34 PM2/22/21
to
Hi,

I am not familiar with class(*), but isn't the "allocate-on-assignment" version
possibly useful for copying two objects having components of class(*) inside?
For example,

type Mydata_t
class(*), allocatable :: data
integer :: n
!! ... other stuff ...
endtype

type(Mydata_t) :: a, b

!! ... b is assumed to be initialized somewhere ...

a = b !! each components are assigned with auto (re)allocation

But I experienced some issue for this kind of "class assignment" some time ago
(so I rewrote the code with "type assignment" only, which had no problem at that time),
so I guess allocate() might be more stable, e.g. for old compiler versions.
0 new messages