From 127744458b0c44905f5a8a0d8226a55199cbb8b9 Mon Sep 17 00:00:00 2001 From: Sebastien Ponce Date: Thu, 21 Jan 2021 17:18:46 +0100 Subject: [PATCH] Better way of filling vector in smartPointer exercise --- code/smartPointers/solution/smartPointers.sol.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/code/smartPointers/solution/smartPointers.sol.cpp b/code/smartPointers/solution/smartPointers.sol.cpp index 96967e3..cee114e 100644 --- a/code/smartPointers/solution/smartPointers.sol.cpp +++ b/code/smartPointers/solution/smartPointers.sol.cpp @@ -50,8 +50,7 @@ void problem1() { try { doStuffWithData(); } catch (std::exception& e) { - std::cerr << "problem1(): Do stuff with data terminated with exception:\n" << e.what() - << "\n We may have a memory leak now." << std::endl; + std::cerr << "problem1(): Do stuff with data terminated with exception:\n" << e.what() << std::endl; } } @@ -155,9 +154,10 @@ void processElement(const LargeObject* /*element*/) { } // model, this becomes a mess. void problem3() { // Let's generate a vector with 10 pointers to LargeObject - std::vector> objVector(10); - for (auto& ptr : objVector) { - ptr.reset(new LargeObject()); + std::vector> objVector; + objVector.reserve(10); + for (unsigned int i = 0; i < 10; i++) { + objVector.emplace_back(new LargeObject()); } // Let's copy it -- GitLab