From: monir on
Hello;

1) An array is declared as:
....Real X(10,20)
and intentionally only a few elements are initialized in the program.

2) Can one correctly assume that the other elements of the array X are
set to zero by the compiler (g95) ??
OR
Are these elements initially undefined ??

3) In some cases I get zero value for these elements, while in other
procedures I get exceptionally large values, and occasionally NaN.

4) If the answer to the subject question is NO, and if it is not
redundant and/or the problem is somewhere else, then one remedy would
be to give zero initial value to the entire array via a DATA
statement, something like:
....Data ((X(i,j),i=1,10), j=1,20) /200*0.0/
or simply by writing the name of the array:
....Data X /200*0.0/

But if the array X is declared as X(m,n), then how would the value
list of the Data statement look like ??
....Data X /m*n*0.0/ !syntax error
....Data X /(m*n)*0.0/ !syntax error

Thank you.
Monir
From: James Van Buskirk on
"monir" <monirg(a)mondenet.com> wrote in message
news:3014b4fe-6037-44b6-9332-72dbe21bb0e0(a)z35g2000yqd.googlegroups.com...

> 1) An array is declared as:
> ...Real X(10,20)
> and intentionally only a few elements are initialized in the program.

> 2) Can one correctly assume that the other elements of the array X are
> set to zero by the compiler (g95) ??
> OR
> Are these elements initially undefined ??

> 3) In some cases I get zero value for these elements, while in other
> procedures I get exceptionally large values, and occasionally NaN.

> 4) If the answer to the subject question is NO, and if it is not
> redundant and/or the problem is somewhere else, then one remedy would
> be to give zero initial value to the entire array via a DATA
> statement, something like:
> ...Data ((X(i,j),i=1,10), j=1,20) /200*0.0/
> or simply by writing the name of the array:
> ...Data X /200*0.0/

> But if the array X is declared as X(m,n), then how would the value
> list of the Data statement look like ??
> ...Data X /m*n*0.0/ !syntax error
> ...Data X /(m*n)*0.0/ !syntax error

The answer to question 2 is that the other elements are initially
undefined.

Now if the values you initialize are initialized with DATA
statements, then I can't think of an elegant way to initialize
the other values to zero because the elegant ways seem to
always create duplicate initial values, which is not allowed.

Assuming the nontrivial values are initialized with assignment
statements (which is not described as "initialization" in the
standard), then you could start out with an assignment statement
such as:

X = 0

which is what I would prefer. Initialization, either by providing
an initializer in the variable declaration:

integer, parameter :: m = 10, n = 20
real :: X(m,n) = 0

or in a DATA statement:

integer, parameter :: m = 10, n = 20
integer, parameter :: mXn = m*n
real :: X(m,n)
data X/mXn*0.0/

is subtly different than when X has no initializer. The difference
arises when X is a local variable in a recursive subprogram.
Providing an initializer, even by initializing one a single element
of array X via a DATA statement, gives X the SAVE attribute which
means that all instances of the subprogram share the same array X,
whereas without the initializer different instances of the
subprogram each have their own private copy of X. Might not matter
if you never use recursive subprograms, but I think they are useful
enough that it's worthwhile to practice with them a little and see
the difference between variables which have the SAVE attribute and
those that don't. When you see the effect of the SAVE attribute
in practice, you may see why I have the attitude about carelessly
using DATA statements without taking into account this side-effect.

--
write(*,*) transfer((/17.392111325966148d0,6.5794487871554595D-85, &
6.0134700243160014d-154/),(/'x'/)); end


From: glen herrmannsfeldt on
monir <monirg(a)mondenet.com> wrote:

> 1) An array is declared as:
> ...Real X(10,20)
> and intentionally only a few elements are initialized in the program.

You mean with a DATA statement?

> 2) Can one correctly assume that the other elements of the array X are
> set to zero by the compiler (g95) ??
> OR
> Are these elements initially undefined ??

It might be that one some systems initialization is all or nothing,
and that the compiler would fill the other positions with zero.
As far as I know, the standard doesn't require that.

> 3) In some cases I get zero value for these elements, while in other
> procedures I get exceptionally large values, and occasionally NaN.

Some systems zero all static variables not otherwise initialized.
(It is required for C, and sometimes happens for compilers based
on a C compiler back end.)

> 4) If the answer to the subject question is NO, and if it is not
> redundant and/or the problem is somewhere else, then one remedy would
> be to give zero initial value to the entire array via a DATA
> statement, something like:
> ...Data ((X(i,j),i=1,10), j=1,20) /200*0.0/
> or simply by writing the name of the array:
> ...Data X /200*0.0/

Unless you really need an initialized static array, I don't
recommend doing it that way. You sometimes get very large
object programs and executable files as all those zeros go
into the file. Some have a way to compress zeros, but not
other values.

> But if the array X is declared as X(m,n), then how would the value
> list of the Data statement look like ??
> ...Data X /m*n*0.0/ !syntax error
> ...Data X /(m*n)*0.0/ !syntax error

Maybe:

integer, parameter :: mn=m*n
data X/mn*0.0/

-- glen
From: robin on
"monir" <monirg(a)mondenet.com> wrote in message news:3014b4fe-6037-44b6-9332-72dbe21bb0e0(a)z35g2000yqd.googlegroups.com...
| Hello;
|
| 1) An array is declared as:
| ...Real X(10,20)
| and intentionally only a few elements are initialized in the program.
|
| 2) Can one correctly assume that the other elements of the array X are
| set to zero by the compiler (g95) ??

I can't say about g95, but in general, no.

| OR
| Are these elements initially undefined ??

Yes.

| 3) In some cases I get zero value for these elements, while in other
| procedures I get exceptionally large values, and occasionally NaN.

Naturally. Those values are junk left beghind by som other program.

| 4) If the answer to the subject question is NO, and if it is not
| redundant and/or the problem is somewhere else, then one remedy would
| be to give zero initial value to the entire array via a DATA
| statement, something like:
| ...Data ((X(i,j),i=1,10), j=1,20) /200*0.0/
| or simply by writing the name of the array:
| ...Data X /200*0.0/

Or by X = 0

| But if the array X is declared as X(m,n), then how would the value
| list of the Data statement look like ??
| ...Data X /m*n*0.0/ !syntax error
| ...Data X /(m*n)*0.0/ !syntax error
|
| Thank you.
| Monir


From: Gordon Sande on
On 2010-03-07 02:33:02 -0400, monir <monirg(a)mondenet.com> said:

> Hello;
>
> 1) An array is declared as:
> ...Real X(10,20)

Is it also a dummy argument? The answer depends a lot on the answer to
this critical question! as is said MANY MANY times, declarations matter.
This is an important part of the declaration.

> and intentionally only a few elements are initialized in the program.
>
> 2) Can one correctly assume that the other elements of the array X are
> set to zero by the compiler (g95) ??
> OR
> Are these elements initially undefined ??

If dummy then same as before the call and arguement association but
otherwise no in general. Some compilers in the past set things to zero.
To provide comapatability with this some will now do it with compiler
options. Some will set things to NaNs, etc, ...

> 3) In some cases I get zero value for these elements, while in other
> procedures I get exceptionally large values, and occasionally NaN.

This answers your earlier question if you had thought about it. Or did
you think the funny values came from some other programming error?

You mention procedures so is this a question about dummy arguements?
See query above.

> 4) If the answer to the subject question is NO, and if it is not
> redundant and/or the problem is somewhere else, then one remedy would
> be to give zero initial value to the entire array via a DATA
> statement, something like:
> ...Data ((X(i,j),i=1,10), j=1,20) /200*0.0/
> or simply by writing the name of the array:
> ...Data X /200*0.0/

Data will set the values exactly ONCE before execution which may or
may not be what you want so you are better off with some form of
assignment. F90 has an array assignment that will do the job.
Much better to explicily make the variables take the values you
intend. The compiler is not required to read your mind and is allowed
to roll the dice any way that strikes its fancy in such matters!

> But if the array X is declared as X(m,n), then how would the value
> list of the Data statement look like ??

If X is a dummy arguement then it can not have a data statement.

> ...Data X /m*n*0.0/ !syntax error
> ...Data X /(m*n)*0.0/ !syntax error

Just exactly was the error message? It might have provided some
hint as to whether X was a dummy or not or whether the problem was
with the n*m. Or more likely it just gae up and said so version of fouey!

Play games with parameters if n and m are also parameters but if
they are dummy arguements then you are out of luck except for an
array assignment.

> Thank you.
> Monir