0

What is the syntax in modern Fortran to declare an array without giving its length and letting the compiler determine the length from the declaration?

The following examples don't work:

program ONE
  real :: V = [1,2,3]
end program

program ONE
  real :: V(*) = [1,2,3]
end program

but this example does work:

program ONE
  real :: V(3) = [1,2,3]
end program

Why can't the compiler calculate the required length from the declaration? Is there a way to do this?

Chris
  • 44,602
  • 16
  • 137
  • 156
Robert H
  • 1,603
  • 2
  • 17
  • 19

4 Answers4

5

You could do the following, which requires two lines, a declaration and an executable statement:

program ONE
  real, dimension (:), allocatable :: V
  V =  [1,2,3]
end program

This makes use of the Fortran 2003 feature of reallocation on assignment.

M. S. B.
  • 28,968
  • 2
  • 46
  • 73
  • 1
    Presumably this *requires* the `dimension(:)` attribute? So it is important to emphasise that you at least need to specify the rank of your array. So you couldn't reallocate a 1D array to a 2D array. – Chris Feb 20 '12 at 14:33
  • 2
    Correct. However, you can use pointers to have arrays of different rank be the same target. This still requires the pointers to be declared with the desired rank. See http://stackoverflow.com/questions/5406016/changing-array-dimensions-in-fortran – M. S. B. Feb 20 '12 at 14:52
  • HELLO: does allocatable mean I need to deallocate it as well at the end, like with c new() and free()? – Robert H Feb 20 '12 at 16:43
  • I think for this to work with Intel fortran you need to compile with `-assume realloc_lhs` (there may also be a compiler declaration to same effect). – laxxy Feb 20 '12 at 19:38
  • 1
    @Robert H, yes it is best to deallocate the allocatable variables when you are finished with them. It is not necessary in procedures (subroutines and functions) since Fortran 95 but doesn't hurt -- local allocatables are automatically deallocated upon exit from procedures. – M. S. B. Feb 20 '12 at 19:50
2

As others have noted, what you want to do is currently not possible in Fortran with variables. I want to note, however, that Fortran 2008 added the possibility of doing this for named constants, declared with the parameter attribute. This is called an 'implied-shape array' and would look like this:

program ONE
  real, parameter :: V(*) = [1,2,3]
end program

It is also possible to specify a lower bound for the array:

program TWO
  real, parameter :: V(2:*) = [1,2,3]
end program

Even though there aren't any Fortran 2008 compilers yet that I have heard of, many compilers will probably already have implemented this, as it doesn't seem to be too difficult.

Perhaps, if an update to the current standard gets published in a couple of years, this will also become possible for variables, although one might imagine that it would have been done right away, at the same time as for named constants, if it was easy.

eriktous
  • 6,569
  • 2
  • 25
  • 35
1

I have never come across the existence of what you seem to be after, certainly not in the context of the examples you have provided. However (but you are probably well aware of this), things are different if you look at subroutines and functions (i.e. when your array becomes a dummy argument), as here you can make use of assumed-shape arrays, as in

subroutine ONE (V)
   real :: V(:)
end subroutine ONE

In the above case the subroutine's V will have the same number of elements as the array you are passing to ONE. Outside the subroutine, however, you do have to declare V using the required number of elements (or make it allocatable and only allocate it once you know how many elements it should have).

I hope this helped.

canavanin
  • 2,559
  • 3
  • 25
  • 36
0

No, you have to declare it one way or the other. Explicitly or (let's say) implicitly, but you have to declare it.

What you are looking for is more of a characteristic of dynamic languages.

You can have allocatable arrays, whose size is not known at compile time, but you cannot have an array whose size is not known at the time of assigning values to it.

Rook
  • 60,248
  • 49
  • 165
  • 242
  • HELLO: You say "whose size is not known at compile time". But its size is known at compile time. does the fortran compiler not able to count the size? When it parses the line, it can see the size of the vector. It can simply count how many elements between the first '[' and the second ']' to know. Why do you think it is hard for the compiler to count in this case? that is what I do not understand. – Robert H Feb 20 '12 at 22:49
  • @RobertH - Think about it. What if you declared an array (set aside the memory for it) yet never used it? – Rook Feb 20 '12 at 22:54
  • Compiler does't "count the size" (unless told otherwise) - it just sets aside the memory required (so to speak). You can go outside those memory bounds if you wish (out of bounds), or are not careful. – Rook Feb 20 '12 at 22:57
  • @idigas, I think you have no clue what you are talking about. may be in Fortran the compiler can't count, but in many other compiled language, they can count very easily and one can do the above declaration in them with no problem. – Robert H Feb 21 '12 at 06:19
  • @RobertH - What particular languges are you reffering to? – Rook Feb 21 '12 at 13:17
  • many. Here is Java: `int[] A = { 1, 1, 5,7 };` if you learn other languages and not just be stuck to Fortran you'll find the world has moved on from 60 years ago. Java compiler can determine the size of A simply by counting. Search the net and you'll find many modern languages with compilers that can count. – Robert H Feb 21 '12 at 15:05
  • 1
    @RobertH - Indeed it has, and though Fortran is 50 years old (for those of us who know how to count), its latest revision is no more than a few years behind us (2008). In any case, no need to take such a tone, it will gain you nothing. I've long stopped comparing languages just for the sake of comparison. Java and Fortran differ in many ways, and admittedly I've not used it much, but from what I know of it, it is not so suited for the kind of work I use Fortran for, therefore making the very comparison moot. If you feel it is better suited for your needs, then what is stpping you frm using it? – Rook Feb 21 '12 at 15:17
  • funny that you complain about comparing languages, yet you are the one who asked me to tell you what other language can do this, and I simply showed you. I use many languages, and I can see better this way the weak points in a language more than those who are stuck using one language only and think it is the best thing out there since they know nothing else. – Robert H Feb 21 '12 at 17:24
  • @RobertH - No, you misunderstood. I was curious of what language you were previously reffering to, to better understand what feature you were looking for since "counting the array" isn't really an exact description of it and it wasn't giving me the exact idea of what you wanted to achieve. I definitely do not agree, less alone advocate the usage of one tool for all tasks. From your wishes it looks perhaps you would be the most happy with a semi dynamic language like MATLAB, if its set of features suit you for what problem you're trying to solve. – Rook Feb 21 '12 at 17:50