2-5 VARIABLE SIZE ARRAYS
*************************
FORTRAN 77 array sizes are determined at the time of compilation,
the 'top declaration' of the array allocates a chunk of memory,
and the size of that memory chunk can't be changed during runtime.
Sometimes we don't know beforehand the array size, because the size
of the input data is unpredictable or inconstant.
There are some methods to solve this problem in the framework of
FORTRAN 77, two of them standard conforming and somewhat limited,
the others are non-standard.
Fortran 90 support of automatic and allocateable arrays solved
this problem in a much better and portable way.
Standard-conforming methods
---------------------------
The idea behind these methods is simple: declare in the calling
procedure an array larger than the maximum expected size and
use only part of it in the called procedure.
There are two options now:
1) Pass the 'physical' and 'logical' array dimensions,
('physical' means here the original dimensions, and
'logical' are the reduced dimensions).
Declare the array with the 'physical' dimensions and
Use the dimensional variables to explicitly limit all
LOOP CONTROL VARIABLEs to the 'logical' dimensions.
2) Re-define the array in the called subroutine to have
the required size. Be careful, after once writing to a
re-defined multi-dimensional array, all the procedures
referencing it must re-define it to the same dimensions
(or compute manually all indices).
By the way, re-definition of the dimensions of an array passed
to a procedure, is valid according to the FORTRAN 77 standard.
From the point of view of memory utilization (minimization of
memory paging, at least for multi-dimensional arrays) it is
preferred to redefine the array dimensions. In this method we
use the original array as a kind of memory pool only.
Both options use the syntax of adjustable arrays.
Example of the physical/logical method
======================================
program phylog
integer nphys, nlog, i
parameter (nphys = 100)
real a(nphys)
do i = 1, nphys
a(i) = i
enddo
write(*,*) 'enter array size: '
read(*,*) nlog
if (nlog .gt. nphys) stop 'too large passed array '
call sub(a, nphys, nlog)
end
subroutine sub(a, nphys, nlog)
integer nphys, nlog, i
real a(nphys)
do i = 1, nlog
write (*,*) i, a(i)
enddo
return
end
Example of the re-definition method
===================================
program recnfg
integer nphys, nlog, i
parameter (nphys = 100)
real a(nphys)
do i = 1, nphys
a(i) = i
enddo
write(*,*) 'enter array size: '
read(*,*) nlog
if (nlog .gt. nphys) stop 'too large passed array '
call sub(a, nlog)
end
subroutine sub(a, n)
integer n, i
real a(n)
do i = 1, n
write (*,*) i, a(i)
enddo
return
end
A more fancy example for the redefinition technique:
====================================================
program adjarr
integer i, j, m, n, a(9,9)
do i=1,9
do j=1,9
a(i,j) = 10*i + j
enddo
enddo
write(*,*) ' '
write(*,*) ' we have an integer array of size 9x9 '
write(*,*) ' array(i,j) = (10 * i) + j '
write(*,*) ' '
write(*,*) ' we will pass it to a subroutine with '
write(*,*) ' the adjustable array mechanism. '
write(*,*) ' the array dimensions will be '
write(*,*) ' re-defined to m x n '
write(*,*) ' '
write(*,*) ' please supply m,n the new '
write(*,*) ' dimensions in the range [1-9] '
write(*,*) ' '
write(*,*) 'enter m: '
read(*,*) m
write(*,*) 'enter n: '
read(*,*) n
call sub(m,n,a)
end
subroutine sub(m,n,a)
integer m, n, a(m,n)
write(*,*)
write(*,*) ' the re-defined array is: '
write(*,*)
write(*,'(1x,i4)') a
return
end
The variable format used in the last WRITE statement is non-standard.
Non-standard methods
--------------------
All these solutions are based on the ability of most operating systems
to allocate a contiguous area of memory to a running program upon request.
Operating systems supply non-standard (from FORTRAN point of view)
'system calls' that a program can utilize to request a memory chunk
while running.
A better solution is to use the memory allocating functions of C,
of course they are implemented with those system calls, but the C
interface is standard (C standard).
The following examples will work when certain requirements are met,
we will try to make them explicit as possible.
The classical 'non-contiguous array' trick
------------------------------------------
The main-program allocates the memory for the 'client procedure',
where a few simple manipulation are performed on the newly created
array, just to prove the method works. Passing the array element
A(OFFSET) to a subroutine, make it easier to access the 'array extension'.
We declare an array A with one element, the 'mem' routine allocates
memory for 100 elements the size of each is 4 bytes. 'mem' returns
an 'array index' OFFSET, computed with 'pointer arithmetic', that
integer is the 'start index' of the 'array extension'. The routine
'unmem', of course, deallocates the new memory area.
Here we probably assume a call-by-reference argument passing mechanism,
and our INTEGERs are 4 bytes long. Another assumption is that the
Fortran compiler doesn't append an underscore to external names
(see the chapter on Fortran/C interfacing).
program dynarr
integer*4 a(1), offset
call mem(a, 100, 4, offset)
call sub(a(offset))
call unmem(a, 100, 4, offset)
end
subroutine sub(a)
integer a(100), i
do i = 1, 100
a(i) = i
enddo
write(*,*)
write(*,*) a
return
end
#include <stdlib.h>
#include <stdio.h>
void mem(int *a, int *m, int *n, int *b)
{
int *new;
new = (int *)calloc(*m, *n);
if (new == NULL)
{
printf("\n not enough memory \n");
exit(1);
}
else
*b = (int)(new - a) + 1;
}
void unmem(int *a, int *m, int *n, int *b)
{
free(a + *b - 1);
}
Another example, a 5x5 two-dimensional INTEGER*2 array is allocated
and initialized in a C routine:
program test
integer m, n, offset
Integer*2 matrix(1)
call mem(matrix, offset)
m = 5
n = 5
call sub(m, n, matrix(offset))
end
A FORTRAN routine (just for testing):
subroutine sub(m, n, matrix)
integer m,n, i
integer*2 matrix(m,n)
write (*,*)
write (*,*) ' In Fortran: '
write (*,*)
write (*,'(1X,5I11)') ((matrix(i,j), j=1,5), i=1,5)
return
end
The C routine can't modify the value of the pointer to "matrix",
so we use the non-contiguous array technique, and compute the
"offset" between a one-element array declared in the Fortran
code and the new allocated array.
We use the allocated array in sub-procedures of the procedure
that called the C routine, by passing to them "matrix(offset)"
which is a "pointer" to the new array computed by f77.
The whole method can be easily made flexible, and you can have
different size for the array on each run (with the adjustable
array syntax).
#include <stdio.h>
#include <stdlib.h>
mem(short **matrix, long *offset)
{
long m, i, j;
short *ptr, **aux;
ptr = (short *) malloc(25 * sizeof(short));
*offset = (long)(ptr - (short *)matrix + 1);
aux = (short **) malloc(5 * sizeof(short *));
for(m = 0 ; m < 5 ; m++)
*(aux + m) = (short *)(ptr + (5 * m));
printf("\n In the C routine: \n");
for(i = 0 ; i < 5 ; i++)
{
printf("\n");
for(j = 0 ; j < 5 ; j++)
{
aux[i][j] = (i+1) * 10 + (j+1);
printf("%11d", aux[i][j]);
}
}
printf("\n");
return;
}
Note that the C routine allocate the storage actually used in the
Fortran code in the first "malloc", it computes the offset between
the one-element Fortran array and the new array with a bit of
pointer arithmetic and passes it back to the Fortran code.
The output looks like this:
In the C routine:
11 12 13 14 15
21 22 23 24 25
31 32 33 34 35
41 42 43 44 45
51 52 53 54 55
In Fortran:
11 21 31 41 51
12 22 32 42 52
13 23 33 43 53
14 24 34 44 54
15 25 35 45 55
Note that printing "row by row" produces
transposed results!
Some remarks on the C routines used above
-----------------------------------------
On most UNIX machines, you will have to add a trailing underscore to
the routine names (mem_, unmem_). See the chapter on FORTRAN/C
interface, or just add the underscore if the compiler/linker complains.
Old C compilers (non-ANSI), declare the dummy (formal) arguments types
not inside the argument list, but in a separate line just below it.
Explicitly controlling the parameter-passing-mechanisms
-------------------------------------------------------
Here we probably assume a call-by-reference argument passing mechanism,
and:
1) The existence of language extensions that control
the mechanism of parameter-passing (In this example
DEC fortran extensions are assumed).
2) The pointer returned by 'malloc' can be stored
in an INTEGER variable and then passed intact
to a FORTRAN procedure (valid in most machines?).
program dynarr
integer m, n, ptr
write(*,*) ' enter two integers: '
read(*,*) m, n
ptr = malloc(%val(m*n))
if (ptr .eq. 0) stop 'not enough memory '
call sub(%val(ptr), m, n)
end
subroutine sub(arr, m, n)
integer m, n, arr(m,n), i, j
do j = 1, n
do i = 1, m
arr(i,j) = 10*i + j
enddo
enddo
write(*, '(1x,i5)') arr
return
end
Many compilers support Cray pointers, these beasts may be used
for memory allocation, but be careful, using Cray pointers for
in computation may cause erroneous results (when automatic
compiler optimization is turned on - the default).
A much more important factor in the social movement than those already mentioned was the ever-increasing influence of women. This probably stood at the lowest point to which it has ever fallen, during the classic age of Greek life and thought. In the history of Thucydides, so far as it forms a connected series of events, four times only during a period of nearly seventy years does a woman cross the scene. In each instance her apparition only lasts for a moment. In three of the four instances she is a queen or a princess, and belongs either to the half-barbarous kingdoms of northern Hellas or to wholly barbarous Thrace. In the one remaining instance208— that of the woman who helps some of the trapped Thebans to make their escape from Plataea—while her deed of mercy will live for ever, her name is for ever lost.319 But no sooner did philosophy abandon physics for ethics and religion than the importance of those subjects to women was perceived, first by Socrates, and after him by Xenophon and Plato. Women are said to have attended Plato’s lectures disguised as men. Women formed part of the circle which gathered round Epicurus in his suburban retreat. Others aspired not only to learn but to teach. Arêtê, the daughter of Aristippus, handed on the Cyrenaic doctrine to her son, the younger Aristippus. Hipparchia, the wife of Crates the Cynic, earned a place among the representatives of his school. But all these were exceptions; some of them belonged to the class of Hetaerae; and philosophy, although it might address itself to them, remained unaffected by their influence. The case was widely different in Rome, where women were far more highly honoured than in Greece;320 and even if the prominent part assigned to them in the legendary history of the city be a proof, among others, of its untrustworthiness, still that such stories should be thought worth inventing and preserving is an indirect proof of the extent to which feminine influence prevailed. With the loss of political liberty, their importance, as always happens at such a conjuncture, was considerably increased. Under a personal government there is far more scope for intrigue than where law is king; and as intriguers women are at least the209 equals of men. Moreover, they profited fully by the levelling tendencies of the age. One great service of the imperial jurisconsults was to remove some of the disabilities under which women formerly suffered. According to the old law, they were placed under male guardianship through their whole life, but this restraint was first reduced to a legal fiction by compelling the guardian to do what they wished, and at last it was entirely abolished. Their powers both of inheritance and bequest were extended; they frequently possessed immense wealth; and their wealth was sometimes expended for purposes of public munificence. Their social freedom seems to have been unlimited, and they formed combinations among themselves which probably served to increase their general influence.321 The old religions of Greece and Italy were essentially oracular. While inculcating the existence of supernatural beings, and prescribing the modes according to which such beings were to be worshipped, they paid most attention to the interpretation of the signs by which either future events in general, or the consequences of particular actions, were supposed to be divinely revealed. Of these intimations, some were given to the whole world, so that he who ran might read, others were reserved for certain favoured localities, and only communicated through the appointed ministers of the god. The Delphic oracle in particular enjoyed an enormous reputation both among Greeks and barbarians for guidance afforded under the latter conditions; and during a considerable period it may even be said to have directed the course of Hellenic civilisation. It was also under this form that supernatural religion suffered most injury from the great intellectual movement which followed the Persian wars. Men who had learned to study the constant sequences of Nature for themselves, and to shape their conduct according to fixed principles of prudence or of justice, either thought it irreverent to trouble the god about questions on which they were competent to form an opinion for themselves, or did not choose to place a well-considered scheme at the mercy of his possibly interested responses. That such a revolution occurred about the middle of the fifth century B.C., seems proved by the great change of tone in reference to this subject which one perceives on passing from Aeschylus to Sophocles. That anyone should question the veracity of an oracle is a supposition which never crosses the mind of the elder dramatist. A knowledge of augury counts among the greatest benefits222 conferred by Prometheus on mankind, and the Titan brings Zeus himself to terms by his acquaintance with the secrets of destiny. Sophocles, on the other hand, evidently has to deal with a sceptical generation, despising prophecies and needing to be warned of the fearful consequences brought about by neglecting their injunctions. The stranger had a pleasant, round face, with eyes that twinkled in spite of the creases around them that showed worry. No wonder he was worried, Sandy thought: having deserted the craft they had foiled in its attempt to get the gems, the man had returned from some short foray to discover his craft replaced by another. “Thanks,” Dick retorted, without smiling. When they reached him, in the dying glow of the flashlight Dick trained on a body lying in a heap, they identified the man who had been warned by his gypsy fortune teller to “look out for a hidden enemy.” He was lying at full length in the mould and leaves. "But that is sport," she answered carelessly. On the retirement of Townshend, Walpole reigned supreme and without a rival in the Cabinet. Henry Pelham was made Secretary at War; Compton Earl of Wilmington Privy Seal. He left foreign affairs chiefly to Stanhope, now Lord Harrington, and to the Duke of Newcastle, impressing on them by all means to avoid quarrels with foreign Powers, and maintain the blessings of peace. With all the faults of Walpole, this was the praise of his political system, which system, on the meeting of Parliament in the spring of 1731, was violently attacked by Wyndham and Pulteney, on the plea that we were making ruinous treaties, and sacrificing British interests, in order to benefit Hanover, the eternal millstone round the neck of England. Pulteney and Bolingbroke carried the same attack into the pages of The Craftsman, but they failed to move Walpole, or to shake his power. The English Government, instead of treating Wilkes with a dignified indifference, was weak enough to show how deeply it was touched by him, dismissed him from his commission of Colonel of the Buckinghamshire Militia, and treated Lord Temple as an abettor of his, by depriving him of the Lord-Lieutenancy of the same county, and striking his name from the list of Privy Councillors, giving the Lord-Lieutenancy to Dashwood, now Lord Le Despencer. "I tell you what I'll do," said the Deacon, after a little consideration. "I feel as if both Si and you kin stand a little more'n you had yesterday. I'll cook two to-day. We'll send a big cupful over to Capt. McGillicuddy. That'll leave us two for to-morrer. After that we'll have to trust to Providence." "Indeed you won't," said the Surgeon decisively. "You'll go straight home, and stay there until you are well. You won't be fit for duty for at least a month yet, if then. If you went out into camp now you would have a relapse, and be dead inside of a week. The country between here and Chattanooga is dotted with the graves of men who have been sent back to the front too soon." "Adone do wud that—though you sound more as if you wur in a black temper wud me than as if you pitied me." "Wot about this gal he's married?" "Don't come any further." "Davy, it 'ud be cruel of us to go and leave him." "Insolent priest!" interrupted De Boteler, "do you dare to justify what you have done? Now, by my faith, if you had with proper humility acknowledged your fault and sued for pardon—pardon you should have had. But now, you leave this castle instantly. I will teach you that De Boteler will yet be master of his own house, and his own vassals. And here I swear (and the baron of Sudley uttered an imprecation) that, for your meddling knavery, no priest or monk shall ever again abide here. If the varlets want to shrieve, they can go to the Abbey; and if they want to hear mass, a priest can come from Winchcombe. But never shall another of your meddling fraternity abide at Sudley while Roland de Boteler is its lord." "My lord," said Edith, in her defence, "this woman has sworn falsely. The medicine I gave was a sovereign remedy, if given as I ordered. Ten drops would have saved the child's life; but the contents of the phial destroyed it. The words I uttered were prayers for the life of the child. My children, and all who know me, can bear witness that I have a custom of asking His blessing upon all I take in hand. I raised my eyes towards heaven, and muttered words; but, my lord, they were words of prayer—and I looked up as I prayed, to the footstool of the Lord. But it is in vain to contend: the malice of the wicked will triumph, and Edith Holgrave, who even in thought never harmed one of God's creatures, must be sacrificed to cover the guilt, or hide the thoughtlessness of another." "Aye, Sir Treasurer, thou hast reason to sink thy head! Thy odious poll-tax has mingled vengeance—nay, blood—with the cry of the bond." HoME古一级毛片免费观看
ENTER NUMBET 0017 www.gaosi4.net.cn www.yeya6.net.cn www.sunics.com.cn www.juxi0.net.cn www.geze8.net.cn www.askwiy.net.cn sicai6.net.cn www.cdzxkj.com.cn www.baoke9.com.cn 5gaas.com.cn