Dangerous Pointer
Loading...
Searching...
No Matches
main.cpp
Go to the documentation of this file.
1#include <cassert>
2#include <iostream>
3#include <numeric> // iota
4
5using namespace std;
6
7
14void init_Carray(int n, double *x);
15
16void init_Carray(int n, double *x)
17{
18 assert(n >= 0);
19 iota(x,x+n,3);
20}
21
28//void print_Carray(int n, double const * const x)
29void print_Carray(int n, double const x[]) // hidden pointer x
30{
31 assert(n >= 0);
32 for (int k=0; k<n; ++k)
33 {
34 cout << x[k] << " ";
35 }
36 cout << endl;
37 delete[] x; // dangerous side effect
38}
39
40
41int main()
42{
43 cout << "Dangerous Pointer" << endl;
44 int n;
45 cout << "Number of elements n = "; cin >>n;
46
47 double* const v = new double [n];
48 init_Carray(n,v);
49
50 print_Carray(n,v);
51
52 v[0] = -3;
53
54 print_Carray(n,v);
55
56 delete[] v;
57
58 return 0;
59}
void init_Carray(int n, double *x)
Initializes a dynamic C array.
Definition main.cpp:16
void print_Carray(int n, double const x[])
Print a dynamic C array into the console.
Definition main.cpp:29
int main()
Definition main.cpp:41