Showing results for 
Search instead for 
Did you mean: 
New Contributor I

Assiging array to lhs without changing size of left hand size.

I need to know if a compiler directive exists that will let me do the following:


a$s%num_funds = a$s%num_funds + 1
call move_alloc(a$s%fund_label, t_fund_label)
a$s%fund_label = t_fund_label
a$s%fund_label(a$s%num_funds) = 'Extra Added'

I increased the size of a$s%fund_labels plus 1, but when I copy the original data back it changes the allocation of a$s%fund_label.  I believe that I found an article about compiler settings to change that, but I just cannot find those settings in my project.




0 Kudos
4 Replies
Black Belt

Why not keep the code in agreement with the standard, and write as follows?

     a$s%fund_label(:a$s%num_funds-1) = t_fund_label

The array has been extended in size by 1, and you have saved the old contents in t_fund_label, so the two sides of this statement conform, and thus a$s%fundlabel will not be reallocated.

Something undesirable remains: you are copying the values twice: first from the old a$s%fund_label to t_fund_label, and then back to the new a$s%fund_label. The purpose of move_alloc, among other things, is to avoid such double copying. Try to rewrite the code to achieve that saving.

0 Kudos
New Contributor I

Already figured something out that works.

0 Kudos
New Contributor I

Another question.  We do not use initialize variables to zero.  I decided for this new application that I should.  I also set initialize arrays as well as scalers. The array I am enlarging is a defined type and  I am getting random numbers in all of the items that make up the type.  I need them to initialize to 0.  Is there a setting to do that?  I have not seen one.

0 Kudos
Black Belt Retired Employee

On your original question, you can't assign an array of one shape to an array with a different shape. You could prevent the reallocation by doing A(:)=B (see Doctor, it hurts when I do this! ) but if A and B have different numbers of elements, then that's invalid and the results unpredictable.

There is no option to initialize derived type values to zero. You can add an initial value to each component in the derived type declaration, and that will be used when variables of that type are created.

0 Kudos