Shallow Copy vs. Deep Copy
Loading...
Searching...
No Matches
Ex643-warning-test.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
#include <iostream>
10
#include <iomanip>
// setw
11
#include <cstring>
// strcpy, strlen
12
using namespace
std;
13
14
// Let's use a tool to detect memory bugs
15
// ~/progs/lib/rmalloc
16
// g++ -Wno-deprecated Ex643-warning-test.cpp rmalloc.o
17
#define MALLOC_DEBUG
18
#include "rmalloc.h"
19
20
struct
Student2
21
{
22
long
long
int
matrikel
;
23
int
skz
;
24
char
*
pname
, *
pvorname
;
// Pointers in structure
25
};
26
27
int
main
()
28
{
29
Student2
arni, robbi;
30
char
tmp[20];
// temp. input string
31
32
// ---------------------------------------------------------------
33
// Input pvorname
34
35
cout << endl <<
" Vorname : "
;
36
37
// setw guarantes that not more than 20 characters are read
38
// see Schader/Kuhlin, p.96
39
40
cin >> setw(
sizeof
(tmp)) >> tmp;
41
42
// Allocate memory for arni.pvorname
43
44
arni.
pvorname
=
new
char
[strlen(tmp)+1];
// Don't forget "+1
45
strcpy(arni.
pvorname
,tmp);
// and copy input on it
46
47
// ---------------------------------------------------------------
48
// Input pname
49
50
cout << endl <<
" Familienname : "
;
51
cin >> setw(
sizeof
(tmp)) >> tmp;
52
53
// Allocate memory for arni.pname
54
55
arni.
pname
=
new
char
[strlen(tmp)+1];
// Don't forget "+1"
56
strcpy(arni.
pname
,tmp);
// and copy input on it
57
58
// ---------------------------------------------------------------
59
// Input skz
60
61
cout << endl <<
" Studentenkennzahl : "
;
62
cin >> arni.
skz
;
63
64
// ---------------------------------------------------------------
65
// Input matrikel
66
67
cout << endl <<
" Matrikelnummer : "
;
68
cin >> arni.
matrikel
;
69
70
// ---------------------------------------------------------------
71
// rough and w r o n g copying
72
73
robbi = arni;
// copies int, long long int, int*
74
// but pvorname, pname point on dynamical data are posessed by arni
75
76
// ---------------------------------------------------------------
77
// output robbi
78
79
cout << endl <<
"-------- This output is still correct -----------"
<< endl;
80
cout << robbi.
pvorname
<<
" "
<< robbi.
pname
<<
", SKZ: "
;
81
cout << robbi.
skz
<<
" "
<< robbi.
matrikel
<< endl << endl;
82
83
// ---------------------------------------------------------------
84
// Now, we deallocate dynamical data in arni
85
86
delete
[] arni.
pvorname
;
87
delete
[] arni.
pname
;
88
89
// ---------------------------------------------------------------
90
// output robbi
91
92
cout << endl <<
"-------- Even this output may be correct -----------"
<< endl;
93
cout << robbi.
pvorname
<<
" "
<< robbi.
pname
<<
", SKZ: "
;
94
cout << robbi.
skz
<<
" "
<< robbi.
matrikel
<< endl << endl;
95
96
// ---------------------------------------------------------------
97
// Let us allocate some tiny dynamical array
98
char
*tiny;
99
tiny =
new
char
[5];
100
strcpy(tiny,
"tiny"
);
101
102
// ---------------------------------------------------------------
103
// output robbi
104
// Suddenly, robbi.pname == "tiny"
105
106
cout << endl <<
"-------- Hey, what happend to my output!! -----------"
<< endl;
107
cout << robbi.
pvorname
<<
" "
<< robbi.
pname
<<
", SKZ: "
;
108
cout << robbi.
skz
<<
" "
<< robbi.
matrikel
<< endl << endl;
109
110
// ---------------------------------------------------------------
111
// Now, we deallocate dynamical data in robbi
112
113
delete
[] robbi.
pvorname
;
114
delete
[] robbi.
pname
;
115
116
}
117
118
119
120
121
122
123
124
125
main
int main()
Definition
Ex643-warning-test.cpp:27
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-test.cpp
Generated on Fri Jun 13 2025 09:16:26 for Shallow Copy vs. Deep Copy by
1.9.8