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