-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtest01.cpp
94 lines (78 loc) · 1.68 KB
/
test01.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# include <cstdlib>
# include <iostream>
using namespace std;
int main ( );
void f ( int n );
//****************************************************************************80
int main ( )
//****************************************************************************80
//
// Purpose:
//
// MAIN is the main program for TEST01.
//
// Discussion:
//
// TEST01 calls F, which has a memory "leak". This memory leak can be
// detected by VALGRID.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 18 May 2011
//
{
int n = 10;
cout << "\n";
cout << "TEST01\n";
cout << " C++ version.\n";
cout << " A sample code for analysis by VALGRIND.\n";
f ( n );
//
// Terminate.
//
cout << "\n";
cout << "TEST01\n";
cout << " Normal end of execution.\n";
return 0;
}
//****************************************************************************80
void f ( int n )
//****************************************************************************80
//
// Purpose:
//
// F computes N+1 entries of the Fibonacci sequence.
//
// Discussion:
//
// Unfortunately, F only allocates space for N entries. Hence, the
// assignment of a value to the N+1 entry causes a memory leak.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 18 May 2011
//
{
int i;
int *x;
x = ( int * ) malloc ( n * sizeof ( int ) );
x[0] = 1;
cout << " " << 0 << " " << x[0] << "\n";
x[1] = 1;
cout << " " << 1 << " " << x[1] << "\n";
for ( i = 2; i <= n; i++ )
{
x[i] = x[i-1] + x[i-2];
cout << " " << i << " " << x[i] << "\n";
}
delete [] x;
return;
}