Hi,
I have a question about the validity of a construction that is
frequently used e.g. in the Numerical Recipes in FORTRAN 77, but that I
suspect to be the cause of segmentation faults in some cases. But before
modifying all my source codes I would like to know whether this is
really a dangerous construction or not (and the reason for the errors is
something else).
Consider a simple array routine, for example the sum of squares of an array:
SUBROUTINE sqrsum(n,a,o)
INTEGER n,i
DOUBLE PRECISION a(n),o
o=0.d0
DO i=1,n
o=o+a(i)**2
END DO
RETURN
END
Normally you would declare a like this
DOUBLE PRECISION a(100000)
or
INTEGER n,nmax
PARAMETER (nmax=100000)
DOUBLE PRECISION a(nmax)
or so, i.e. using a constant. My question is simply: Is the upper way of
array dimensioning valid (at least in g77) or is it a "dirty hack" that
will work most times but may fail with segmentation faults, and
therefore cannot be recommended?
I know that the best way would be to post a minimal example, but I
suspect that the problem only occurs in more complex programs like the
one I have trouble with.
Ingo
|
|
0
|
|
|
|
Reply
|
ingo.thies (51)
|
7/6/2007 2:08:41 PM |
|
Ingo Thies wrote:
> Hi,
>
> I have a question about the validity of a construction that is
> frequently used e.g. in the Numerical Recipes in FORTRAN 77, but that I
> suspect to be the cause of segmentation faults in some cases. But before
> modifying all my source codes I would like to know whether this is
> really a dangerous construction or not (and the reason for the errors is
> something else).
>
> Consider a simple array routine, for example the sum of squares of an array:
>
> SUBROUTINE sqrsum(n,a,o)
> INTEGER n,i
> DOUBLE PRECISION a(n),o
> o=0.d0
> DO i=1,n
> o=o+a(i)**2
> END DO
> RETURN
> END
>
> Normally you would declare a like this
>
> DOUBLE PRECISION a(100000)
>
> or
> INTEGER n,nmax
> PARAMETER (nmax=100000)
> DOUBLE PRECISION a(nmax)
>
> or so, i.e. using a constant. My question is simply: Is the upper way of
> array dimensioning valid (at least in g77) or is it a "dirty hack" that
> will work most times but may fail with segmentation faults, and
> therefore cannot be recommended?
>
In f66 and f77, the first version was called adjustable dimension. It
remains valid after f90, although the terminology changed, and perhaps
is not well explained in all texts.
As the memory region has to be declared in the calling subroutine, there
is no way to check for bounds violations at compile time, with separate
compilation. Most compilers (even g77) have optional checks for this,
typically at run time. f90 gives you additional options, including some
which don't involve special time-consuming run-time options for bounds
checks.
|
|
0
|
|
|
|
Reply
|
timothyprince1 (449)
|
7/6/2007 2:25:11 PM
|
|
On Jul 6, 10:08 am, Ingo Thies <ingo.th...@gmx.de> wrote:
> SUBROUTINE sqrsum(n,a,o)
> INTEGER n,i
> DOUBLE PRECISION a(n),o
> or so, i.e. using a constant. My question is simply: Is the upper way of
> array dimensioning valid (at least in g77) or is it a "dirty hack" that
> will work most times but may fail with segmentation faults, and
> therefore cannot be recommended?
This is a standard Fortran 77 feature called "adjustable array". The
upper bound for the array A is taken from the integer argument N on
routine entry. It's perfectly fine.
Steve
|
|
0
|
|
|
|
Reply
|
Steve.Lionel (766)
|
7/6/2007 2:27:45 PM
|
|
Steve Lionel wrote:
> This is a standard Fortran 77 feature called "adjustable array". The
> upper bound for the array A is taken from the integer argument N on
> routine entry. It's perfectly fine.
A thanks. This means that there is another error. Meanwhile I probably
have found it, and as I guessed, it was due to the complexity of the
program. In particulac, n has been initialized in the first call of the
problem but not saved for the following calls of the routine that calls
the crashed subroutine.
BTW I remember that there is another way of adjustable arrays using a(*)
or something like that. Is this also valid?
Ingo
|
|
0
|
|
|
|
Reply
|
ingo.thies (51)
|
7/6/2007 2:55:36 PM
|
|
Ingo Thies wrote:
| Steve Lionel wrote:
|
|| This is a standard Fortran 77 feature called "adjustable array". The
|| upper bound for the array A is taken from the integer argument N on
|| routine entry. It's perfectly fine.
|
| A thanks. This means that there is another error. Meanwhile I probably
| have found it, and as I guessed, it was due to the complexity of the
| program. In particulac, n has been initialized in the first call of the
| problem but not saved for the following calls of the routine that calls
| the crashed subroutine.
|
| BTW I remember that there is another way of adjustable arrays using a(*)
| or something like that. Is this also valid?
That form is called "assumed-size" arrays, and it's also valid since F77.
While most compilers implement the two in an identical manner, the
difference comes for debugging and bounds-checking purposes. With
the "adjustable" arrays, the compiler has the possibility to check
whether the bounds are achieved (and debuggers also tend to handle
them in a more user-friendly manner). There are also some differences
pertaining F90 array operations, but they're tangential to the matter.
Of course, if the actual argument corresponding with N in the
calling routine is wrong (denoting more elements than the actual
array size), things will go wrong with adjustable arrays as well.
--
Jugoslav
___________
www.xeffort.com
Please reply to the newsgroup.
You can find my real e-mail on my home page above.
|
|
0
|
|
|
|
Reply
|
jdujic (694)
|
7/6/2007 3:29:07 PM
|
|
On Jul 6, 11:29 am, "Jugoslav Dujic" <jdu...@yahoo.com> wrote:
> Ingo Thies wrote:
> | BTW I remember that there is another way of adjustable arrays using a(*)
> | or something like that. Is this also valid?
>
> That form is called "assumed-size" arrays, and it's also valid since F77.
> While most compilers implement the two in an identical manner, the
> difference comes for debugging and bounds-checking purposes.
Adjustable and assumed-size arrays are not typically implemented the
same. Adjustable arrays can have variables for any of the bounds,
where assumed-size allows the * on only the last upper bound. Also,
adjustable arrays are allowed to be used whole in I/O statements,
since the size is known, whereas assumed-size arrays are not.
While we're talking about assumed-size - in F66 it was common to
dimension a dummy argument array (1) when you didn't know the size.
In F77 you could use (*) for this, but many compilers, even today,
make a special case for (1) and disable bounds checking for (1).
Steve
|
|
0
|
|
|
|
Reply
|
Steve.Lionel (766)
|
7/6/2007 3:46:22 PM
|
|
Steve Lionel wrote:
> On Jul 6, 10:08 am, Ingo Thies <ingo.th...@gmx.de> wrote:
>> SUBROUTINE sqrsum(n,a,o)
>> INTEGER n,i
>> DOUBLE PRECISION a(n),o
>>or so, i.e. using a constant. My question is simply: Is the upper way of
>>array dimensioning valid (at least in g77) or is it a "dirty hack" that
>>will work most times but may fail with segmentation faults, and
>>therefore cannot be recommended?
> This is a standard Fortran 77 feature called "adjustable array". The
> upper bound for the array A is taken from the integer argument N on
> routine entry. It's perfectly fine.
Adjustable arrays in subroutines like this go back to Fortran 66.
The (*) dimension for 1D arrays, or for the last subscript of
higher dimension arrays, came in with Fortran 77.
-- glen
|
|
0
|
|
|
|
Reply
|
gah (12236)
|
7/6/2007 4:44:30 PM
|
|
|
6 Replies
45 Views
(page loaded in 0.132 seconds)
Similiar Articles: changing size of array - comp.soft-sys.matlabHow to: Change the Size of an Array You resize an array variable by assigning a new array object to it. You can use either a standard assignment statement or the ReDim ... How to increase array size by 1 - comp.lang.idl-pvwaveHello I have a 2D array: xyz = fltarr (5,Size) How can I increase the ... comp.lang.idl-pvwave ..... figure out is how to change those values of 0 within the array ... Strange Length of an array ?? - comp.lang.xharbourStrange Length of an array ?? - comp.lang.xharbour "Ron Pinkas" <Ron.Pinkas_remove_this_@xHarbour.com ... size of array - comp.soft-sys.matlab change array size ... Reading an unformatted file - comp.lang.fortranMaybe the size in bytes of an INTEGER differs from ... -- write(*,*) transfer((/17.392111325966148d0,6 ... Reading/writing data to/from files into 2D array - comp ... Large Arrays - Memory Problems - comp.soft-sys.matlabUnfortunately the program I am writing may change the '33' size dimension and I'd like ... changing size of array - comp.soft-sys.matlab... 84 and others are 186 x 84 So I ... how to find the size of each disk - comp.unix.solarisChange sector size - comp.periphs.scsi how to find the size of each disk - comp.unix.solaris changing size of array - comp.soft-sys.matlab how to find the size of each ... Change sector size - comp.periphs.scsiHow can i change the sector size back to the usual 512/skt? Problem is the drive is HVD ... how to find the size of each disk - comp.unix.solaris changing size of array ... syntax for creating array of cell arrays - comp.soft-sys.matlab ...... with actors=struct('globalPose',cell(1,nbActrors)); at this point my structure exists, but the field is an empty 0x0 numeric array. I would now have to change the size ... Local array variables in functions - comp.lang.awk... are passed 'by reference' in awk, and changing an array inside the function will change its ... array) >> print length(global_array) >> print length(some_array ... f77 and dynamic arrays in common blocks - comp.lang.fortran ...Then locate all the small variable-size arrays within this by computing starting ... I suppose you know about the automatic arrays in g77, which are an extension to ... Using and Porting GNU Fortran - GCC, the GNU Compiler Collection ...Array Size. Currently, g77 uses the default INTEGER type for array indexes, which limits the sizes of single-dimension arrays on systems offering a larger address ... Adjustable Arrays - Using and Porting GNU Fortran... cannot (legitimately) change the value of the temporary during execution of the procedure, the size of the array ... to that adjustable array would be expected. The g77 ... 7/15/2012 2:16:23 PM
|