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
5
using namespace
std;
6
7
14
void
init_Carray
(
int
n,
double
*x);
15
16
void
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)
29
void
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
41
int
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
}
init_Carray
void init_Carray(int n, double *x)
Initializes a dynamic C array.
Definition
main.cpp:16
print_Carray
void print_Carray(int n, double const x[])
Print a dynamic C array into the console.
Definition
main.cpp:29
main
int main()
Definition
main.cpp:41
main.cpp
Generated on Fri Jun 13 2025 09:15:48 for Dangerous Pointer by
1.9.8