Вы находитесь на странице: 1из 1

Scientific Programming and Computer Architecture

the second of which includes the header file aitken.h. Including aitken.h allows the compiler to ensure
that the definitions in aitken.c are consistent with the declarations in the header file. The first line includes the
standard header assert.h. The job of finding that header file is left to the compiler. Including that header file
allows us to use the assert statement to check a condition in the body of the code (see line 4 below).
The function aitken() operates on arrays that are passed to it as pointers.
1 void aitken(double* seq1, double* seq2, int len){
2 int i;
3 double a, b, c;
4 assert(len > 2);
5 for(i=0; i < len-2; i++){
6 a = seq1[i];
7 b = seq1[i+1];
8 c = seq1[i+2];
9 seq2[i] = a - (b-a)*(b-a)/(a-
2*b+c);
10 }
11 }
In a function that calls aitken() from some other source file, we may have declared two arrays using
double s[100], t[100]. Those two arrays will correspond to two segments of memory each equal to 100
doubles (see figure 1.4↓). As we have noted, s[0]...s[99] and t[0]...t[99] are names of double locations,
which make up those segments of memory. In contrast, s and t have values of type double * but are not names of
any locations in memory. If a call is made as
aitken(s, t, 100);
it has the following effect. The function parameters seq1 and seq2 are names of locations in memory that
can hold double *. The values of s and t are copied to the locations in memory whose names are seq1 and seq2,
respectively (see figure 1.4↓). The value of seq1+17 is the same as the value of s+17. Thus, seq1[17], which is
exactly the same as *(seq1+17), is another name for the memory location s[17]. Thus, we see that by indexing
into seq1 and seq2 as seq1[0]...seq1[99] and seq2[0]...seq2[99], we may refer to any entry in the arrays s
and t defined by the caller (see figure 1.4↓).

Figure 1.4 Passing arrays as pointers. In this picture, s and t are values but not names of any locations.

There is a little catch here, however. What happens if we say seq1[100] or seq1[200]? We would be
generating a name for a location in memory that was not legally claimed by the caller. That is likely to result in a
run-time error. By just using the pointers seq1 and seq2, there is no way we can tell how long the array is.
Therefore, the length of the array is the third parameter, which is named len, in the function definition. The caller
has to explicitly give the length of the array, as it does here by passing 100 as the third argument.
Line 9 of the listing corresponds directly to the Aitken transformation formula (1.1)↑.
The assert(len>2) statement on line 4 works as follows. If the code is compiled with the option -
DNDEBUG, it is as if that line were not there and no extra overhead is incurred. If that option is not used during
compilation, the condition len>2 is checked during run-time. If it is violated, the program will abort and print a
message indicating the name of the file and the line number of the assertion that turned out to be false. The assert

https://divakarvi.github.io/bk-spca/spca.html[20-1-2019 23:44:49]

Вам также может понравиться