Skip to content

Commit af73f58

Browse files
committed
Add code to show the size of pointers.
1 parent e8e52dc commit af73f58

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

src/smart_pointers_main.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ int main() {
5959
size_t sec_num = {1};
6060

6161
// 1. RAW vs SMART POINTER
62-
std::cout << "==> " << sec_num++ << " RAW vs SMART POINTER:" << std::endl;
62+
std::cout << sec_num++ << ". RAW vs SMART POINTER:" << std::endl;
6363
A *obj_a = new A("Andrzej");
6464
std::unique_ptr<A> obj_b(new A("Jack"));
6565

@@ -68,10 +68,15 @@ int main() {
6868
std::unique_ptr<A> obj_b_copy = obj_b;
6969
#endif
7070

71+
// These two should be identical.
72+
std::cout << "\n(should be identical)\n";
73+
std::cout << "Size of raw pointer: " << sizeof obj_a << std::endl;
74+
std::cout << "Size of unique_ptr: " << sizeof obj_b << std::endl;
75+
7176
delete obj_a;
7277

7378
// 2. SHARED_PTR vs WEAK_PTR
74-
std::cout << "==> " << sec_num++ << " SHARED_PTR vs WEAK_PTR:" << std::endl;
79+
std::cout << std::endl << sec_num++ << ". SHARED_PTR vs WEAK_PTR:" << std::endl;
7580
std::shared_ptr<A> obj_c(new A("David"));
7681
std::shared_ptr<A> obj_c_copy(obj_c);
7782

@@ -80,16 +85,21 @@ int main() {
8085
std::weak_ptr<A> obj_c_weak_ptr(obj_c);
8186
std::cout << "Use count for David: " << obj_c.use_count() << std::endl;
8287

88+
// These two should be identical.
89+
std::cout << "\n(should be smaller than unique and raw pointers)\n";
90+
std::cout << "Size of shared pointer: " << sizeof obj_c << std::endl;
91+
std::cout << "Size of weak pointer: " << sizeof obj_c_weak_ptr << std::endl;
92+
8393
// 3. CIRCULAR DEPENDENCY
84-
std::cout << "==> " << sec_num++ << " CIRCULAR DEPENDENCY" << std::endl;
94+
std::cout << std::endl << sec_num++ << ". CIRCULAR DEPENDENCY" << std::endl;
8595
std::shared_ptr<Player> andrzej(new Player());
8696
std::shared_ptr<Player> jack(new Player());
8797

8898
jack->partner = andrzej;
8999
andrzej->partner = jack;
90100

91101
// 4. MAKE_UNIQUE AND MAKE_SHARED
92-
std::cout << "==> " << sec_num++ << " STD::MAKE_UNIQUE" << std::endl;
102+
std::cout << std::endl << sec_num++ << ". STD::MAKE_UNIQUE" << std::endl;
93103
#if __cplusplus >= 201403L
94104
std::unique_ptr<Player> warzynski = std::make_unique<Player>();
95105
#endif

0 commit comments

Comments
 (0)