Prev: Problems matching between FORTRAN COMMON and C struct defined in a ?dll
Next: Problems matching between FORTRAN COMMON and C struct definedin a dll
From: yaqi on 30 Mar 2010 15:46 Hi, For an integer array, we can do the following: integer, parameter, dimension(2) :: a =(/1, 2/) I guess this array will be in the memory before the process starts. My question is that can we do the similar thing for a user-defined type, for example, type mytype integer, pointer, dimension(:) :: ptr => null() end type mytype type(mytype), parameter :: c = mytype(ptr =>(/1,2/)) (the above line is not compilable, it is used only for illustrating the idea.) Thanks.
From: glen herrmannsfeldt on 30 Mar 2010 16:19 yaqi <yaqiwang(a)gmail.com> wrote: > For an integer array, we can do the following: > integer, parameter, dimension(2) :: a =(/1, 2/) > I guess this array will be in the memory before the process starts. I would say that it isn't necessarily in memory at all. If you want something in memory before the process starts, usually called static allocation, initialize a variable (not a parameter). integer, dimension(2) :: a =(/1, 2/) (with the implied SAVE attribute.) > My question is that can we do the similar thing for a user-defined > type, for example, > type mytype > integer, pointer, dimension(:) :: ptr => null() > end type mytype > type(mytype), parameter :: c = mytype(ptr =>(/1,2/)) > (the above line is not compilable, it is used only for illustrating > the idea.) This leaves about three different questions, all of which I don't know the answer for. 1) Are structure parameters allowed? 2) Are pointer parameters allowed? 3) Are initialized structures allowed? 4) Are pointers to initialized structures allowed? I don't believe that I have tried any of those yet in Fortran, and don't know which are allowed. -- glen
From: yaqi on 30 Mar 2010 16:26 On Mar 30, 1:46 pm, yaqi <yaqiw...(a)gmail.com> wrote: > Hi, > > For an integer array, we can do the following: > > integer, parameter, dimension(2) :: a =(/1, 2/) > > I guess this array will be in the memory before the process starts. > > My question is that can we do the similar thing for a user-defined > type, for example, > > type mytype > integer, pointer, dimension(:) :: ptr => null() > end type mytype > > type(mytype), parameter :: c = mytype(ptr =>(/1,2/)) > (the above line is not compilable, it is used only for illustrating > the idea.) > > Thanks. With Intel Visual Fortran, the following code is allowed, type(mytype), parameter :: c = mytype(null()) But I want c%ptr point to something constant during run-time. I want to set up the constant array during the compiling time although I can do array assignment at the beginning of run-time. Unfortunately, the following is not allowed, type(mytype), parameter :: c = mytype( (/1,2/) ) I found in IVF's documentation "If a component of the derived type is a pointer, the value in the expression list must evaluate to an object that would be a valid target in a pointer assignment statement. (A constant is not a valid target in a pointer assignment statement.)" But it does not help for this.
From: yaqi on 30 Mar 2010 16:33 On Mar 30, 2:19 pm, glen herrmannsfeldt <g...(a)ugcs.caltech.edu> wrote: > yaqi <yaqiw...(a)gmail.com> wrote: > > For an integer array, we can do the following: > > integer, parameter, dimension(2) :: a =(/1, 2/) > > I guess this array will be in the memory before the process starts. > > I would say that it isn't necessarily in memory at all. > > If you want something in memory before the process starts, > usually called static allocation, initialize a variable > (not a parameter). > > integer, dimension(2) :: a =(/1, 2/) > > (with the implied SAVE attribute.) > > > My question is that can we do the similar thing for a user-defined > > type, for example, > > type mytype > > integer, pointer, dimension(:) :: ptr => null() > > end type mytype > > type(mytype), parameter :: c = mytype(ptr =>(/1,2/)) > > (the above line is not compilable, it is used only for illustrating > > the idea.) > > This leaves about three different questions, all of which I > don't know the answer for. > > 1) Are structure parameters allowed? > > 2) Are pointer parameters allowed? > > 3) Are initialized structures allowed? > > 4) Are pointers to initialized structures allowed? > > I don't believe that I have tried any of those yet in > Fortran, and don't know which are allowed. > > -- glen Hi Glen, I've tested the following piece with IVF, type(mytype), parameter :: c = mytype(null()) , which should answer your questions. But still I can not make c%prt point to something meaningful. --yaqi
From: glen herrmannsfeldt on 30 Mar 2010 16:46
yaqi <yaqiwang(a)gmail.com> wrote: (snip) > But I want c%ptr point to something constant during run-time. I want > to set up the constant array during the compiling time although I can > do array assignment at the beginning of run-time. > Unfortunately, the following is not allowed, > type(mytype), parameter :: c = mytype( (/1,2/) ) > I found in IVF's documentation "If a component of the derived type is > a pointer, the value in the expression list must evaluate to an object > that would be a valid target in a pointer assignment statement. (A > constant is not a valid target in a pointer assignment statement.)" > But it does not help for this. Yes it does. You don't want a PARAMETER, but an array with the SAVE attribute. Just as you can't have a pointer to the constant 1.0, you can't have a pointer to a PARAMETER. An initialized pointer, which necessarily has the SAVE attribute, might be able to point to a variable with the SAVE (and TARGET) attribute. (I am not sure, but it seems possible.) It couldn't point to a variable without the SAVE attribute, as that might not have been allocated yet. -- glen |