-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathproblem2.cpp
88 lines (61 loc) · 2.32 KB
/
problem2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <iostream>
#include <array>
#include <vector>
/* --------------------------------------------------------------------------------------------
* Collections of smart pointers.
*
* Often, one has to store pointers to objects in collections.
* Fix the memory leaks below by using `std::unique_ptr`.
*
* Tasks
* 1) Compile and run the code below. Notice that the final count is `10`,
* which is expected because the new objects are never deallocated.
* 2) Factory functions should better return smart pointers,
* because it clarifies who owns an object.
* Change the return type of the function `newLargeObject()` for a `std::unique_ptr()`.
* The vector should own the objects, so try to store them using smart pointers.
* Since the change function doesn't accept smart pointers, find a solution to pass the objects.
* Try to use `std::unique_ptr`, not `std::shared_ptr` !
* --------------------------------------------------------------------------------------------
*/
// The class LargeObject emulates a large object.
// One should avoid to copy it, and rather use
// a pointer to pass it around.
struct LargeObject {
std::array<double, 100000> data ;
// So to check for some potential memory leak,
// we count the constructions and destructions
inline static std::size_t count = 0;
LargeObject() { count++ ; }
~LargeObject() { count-- ; }
} ;
// A factory function to create large objects.
LargeObject * newLargeObject() {
// MAKE YOUR CHANGES IN THIS FUNCTION
auto object = new LargeObject() ;
// Imagine there is more setup steps of "object" here
// ...
return object ;
}
// A function to do something with the objects.
// Note that since we don't own the object,
// we don't need a smart pointer as argument.
void changeLargeObject( LargeObject & object ) {
object.data[0] = 1. ;
}
void doStuff() {
// MAKE YOUR CHANGES IN THIS FUNCTION
std::vector<LargeObject *> largeObjects ;
for ( unsigned int i = 0 ; i < 10 ; ++i ) {
auto newObj = newLargeObject() ;
// ... additional newObj setup ...
largeObjects.push_back(newObj) ;
}
for ( const auto & obj : largeObjects ) {
changeLargeObject(*obj) ;
}
}
int main() {
doStuff() ;
std::cout<<"Leaked large objects: "<<LargeObject::count<<std::endl ;
}