From: mecej4 on
On 5/18/2010 9:10 PM, Hifi-Comp wrote:
> So you are saying there is no way in Fortran to accomplish what I
> intended to do: changing the original global constant n, which used to
> define the dimension of the array, into a user input one, with
> minimized change to the original code.
>
>
> On May 18, 9:56 pm, steve<kar...(a)comcast.net> wrote:
>> On May 18, 6:51 pm, Hifi-Comp<wenbinyu.hea...(a)gmail.com> wrote:
>>
>>
>>
>>
>>
>>> My original question was not clear and get sidetracked. Hence I am
>>> trying to clarify the question:
>>
>>> Currently I have the following:
>>
>>> INTEGER,PARAMETER:: n=3
>>
>>> TYPE,PUBLIC:: User_Data
>>> REAL(DBL_AD)::scale
>>> REAL(DBL_AD)::vector(n)
>>> END TYPE User_Data
>>
>>> TYPE(User_Data):: x, y(30),z(100,10)
>>
>>> In the real situation, I want the user of compiled code to input n
>>> (remain the same throughout the calculation) and with almost no
>>> changes to the original code.
>>
>>> Richard suggested to change the type definition as follows
>>> TYPE,PUBLIC:: User_Data
>>> REAL(DBL_AD)::scale
>>> REAL(DBL_AD),ALLOCATABLE::vector(:)
>>> END TYPE User_Data
>>
>>> However, I don't want to insert allocate statement for each variables
>>> of User_Data as there are many such types in the overall program. It
>>> seems to me I should be able to do something like
>>
>>> ALLOCATE(USER_DATA%VECTOR(N))
>>
>>> to fix the dimension of vector, and all the variables of User_Data
>>> will then know the dimension already. Is it posslbe to achieve this?
>>
>> No.
>>
>> See Richard's previous response.
>>
>> PS: You can't ALLOCATE something that does not have the
>> ALLOCATABLE (or POINTER) attribute.
>>
>> --
>> steve- Hide quoted text -
>>
>> - Show quoted text -
>

That's what he said, and here is an explanation that may help to
overcome your apparent reluctance to accept what he said.

When you use an integer parameter (which is called a "named constant" or
"symbolic constant" in other languages) to declare arrays using that
parameter in their dimensions, the compiler knows the size of the arrays.

If, in place of the integer parameter, you wish to use an integer
variable whose value your users will input, the sizes of the arrays are
not known at compile time. Therefore, the arrays have to be allocated
dynamically, on the stack or the heap, depending on how they are
declared (automatic or allocatable).

There is a reason why the ALLOCATABLE attribute is required in order to
allocate a variable. The compiler has to issue extra code to keep track
of whether ALLOCATED(variable) is true or false, and to keep track of
the size information for allocated objects. For a static object, this
bookkeeping is not needed and the compiled code will be slightly more
efficient.

-- mecej4

From: Gib Bogle on
Hifi-Comp wrote:
> I am basically redefing the intrinsic real numbers with the user typer
> to overload a fortran code may not easy be written by me. I have no
> control of how many real variables (scalars and arrays) and how they
> will be used in the code. I really appreciate all the answers. It
> might be hopeless to solve my problem but I learn more about dynamic
> arrays. Thanks a lot!

Don't top-post.

What is the range of values that you want n to be able to take?