Shallow Copy vs. Deep Copy
Loading...
Searching...
No Matches
Ex643-warning.cpp
Go to the documentation of this file.
1
// Ex643-warning.cpp
2
3
// Sec. 6.4 of lecture
4
// Dynamic arrays in structures
5
6
// Demonstration of w r o n g code
7
// wrt. copying a structure with pointers
8
9
// g++ -g -std=c++11 -Wall -Wextra -pedantic Ex643-warning.cpp
10
// valgrind -v --leak-check=yes --tool=memcheck --undef-value-errors=yes --track-origins=yes --log-file=ex643.addr.out --show-reachable=yes ./a.out
11
12
#include <iostream>
13
#include <iomanip>
// setw
14
#include <cstring>
// strcpy, strlen
15
using namespace
std;
16
17
struct
Student2
18
{
19
long
long
int
matrikel
;
20
int
skz
;
21
char
*
pname
, *
pvorname
;
// Pointers in structure
22
};
23
24
int
main
()
25
{
26
Student2
arni;
27
char
tmp[20];
// temp. input string
28
29
// ---------------------------------------------------------------
30
// Input pvorname
31
32
cout << endl <<
" Vorname : "
;
33
34
// setw guarantes that not more than 20 characters are read
35
// see Schader/Kuhlin, p.96
36
37
cin >> setw(
sizeof
(tmp)) >> tmp;
38
39
// Allocate memory for arni.pvorname
40
41
arni.
pvorname
=
new
char
[strlen(tmp)+1];
// Don't forget "+1
42
strcpy(arni.
pvorname
,tmp);
// and copy input on it
43
44
// ---------------------------------------------------------------
45
// Input pname
46
47
cout << endl <<
" Familienname : "
;
48
cin >> setw(
sizeof
(tmp)) >> tmp;
49
50
// Allocate memory for arni.pname
51
52
arni.
pname
=
new
char
[strlen(tmp)+1];
// Don't forget "+1"
53
strcpy(arni.
pname
,tmp);
// and copy input on it
54
55
// ---------------------------------------------------------------
56
// Input skz
57
58
cout << endl <<
" Studentenkennzahl : "
;
59
cin >> arni.
skz
;
60
61
// ---------------------------------------------------------------
62
// Input matrikel
63
64
cout << endl <<
" Matrikelnummer : "
;
65
cin >> arni.
matrikel
;
66
67
// ---------------------------------------------------------------
68
// rough and w r o n g copying
69
70
const
Student2
robbi = arni;
// copies int, long long int, int*
71
// but pvorname, pname point on dynamical data are posessed by arni
72
73
// ---------------------------------------------------------------
74
// output robbi
75
76
cout << endl <<
"-------- This output is still correct -----------"
<< endl;
77
cout << robbi.
pvorname
<<
" "
<< robbi.
pname
<<
", SKZ: "
;
78
cout << robbi.
skz
<<
" "
<< robbi.
matrikel
<< endl << endl;
79
80
// ---------------------------------------------------------------
81
// Now, we deallocate dynamical data in arni
82
83
delete
[] arni.
pvorname
;
84
delete
[] arni.
pname
;
85
86
// ---------------------------------------------------------------
87
// output robbi
88
89
cout << endl <<
"-------- Even this output might be correct -----------"
<< endl;
90
cout << robbi.
pvorname
<<
" "
<< robbi.
pname
<<
", SKZ: "
;
91
cout << robbi.
skz
<<
" "
<< robbi.
matrikel
<< endl << endl;
92
93
// ---------------------------------------------------------------
94
// Let us allocate some tiny dynamical array
95
char
*tiny;
96
tiny =
new
char
[5];
97
strcpy(tiny,
"tiny"
);
98
99
// ---------------------------------------------------------------
100
// output robbi
101
// Suddenly, robbi.pname == "tiny"
102
103
cout << endl <<
"-------- Hey, what happend to my output!! -----------"
<< endl;
104
cout << robbi.
pvorname
<<
" "
<< robbi.
pname
<<
", SKZ: "
;
105
cout << robbi.
skz
<<
" "
<< robbi.
matrikel
<< endl << endl;
106
107
// ---------------------------------------------------------------
108
// Now, we deallocate dynamical data in robbi
109
110
delete
[] robbi.
pvorname
;
111
delete
[] robbi.
pname
;
112
return
0;
113
}
114
115
116
117
118
119
120
121
122
main
int main()
Definition
Ex643-warning.cpp:24
Student2
Definition
Ex643-correct.cpp:19
Student2::matrikel
long long int matrikel
Definition
Ex643-correct.cpp:20
Student2::pvorname
char * pvorname
Definition
Ex643-correct.cpp:22
Student2::skz
int skz
Definition
Ex643-correct.cpp:21
Student2::pname
char * pname
Definition
Ex643-correct.cpp:22
Ex643-warning.cpp
Generated on Fri Jun 13 2025 09:16:26 for Shallow Copy vs. Deep Copy by
1.9.8