Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions debugging/src/buggy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@ class Student {

Student* getStudent(){
Student* myStudent = new Student(10801983,"Mary Wright");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

given this new statement and seeing no delete anywhere this code has a memory leak.

// ***** OPS!
return nullptr;
return myStudent;
}

void printStudent(Student* myStudent){
std::cout << "-- Student's Data --\n";
std::cout << "Student's ID is " << myStudent->getName()
<< " and name is " << myStudent->getID() << std::cout;
std::cout << "Student's ID is " << myStudent->getID()
<< " and name is " << myStudent->getName() << std::endl;
}

int main(){
Student* myStudent = getStudent();
printStudent(myStudent);
delete myStudent;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you could have used a smart pointer here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way to do this without overwriting all standard pointer usage in the code? Initialising as a smart pointer type breaks printStudent as it expects a raw pointer as input.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return 1;
}
14 changes: 13 additions & 1 deletion modernsyntax/classical_looping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,17 @@ void indexLoop(std::vector<Tool>& tools){
}

void iteratorLoop(std::vector<Tool>& tools){
for(std::vector<Tool>::const_iterator tool = tools.begin(), end = tools.end(); tool != end; ++tool){
for(auto tool = tools.begin(), end = tools.end(); tool != end; ++tool){
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

auto tool creates an unecessary copy of each object. Did you consider references?
In addition the const_iterator is const, while auto alone does not preserve constness.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What exactly does auto tool copy? The input vector itself is already passed by reference.

How can tool be const when it is incremented? Supposedly const_iterator implies the target to be const, which is not consistent with the declaration of the function input.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What you get in hand is the object, not an iterator to it. auto tool copies every single tool you are looping over. auto& tool would prevent that.
And as you are dealing with the object and not an iterator, it can as well be const.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The iterator increment ++tool does not work with const, auto seems to favour the normal iterator structure over const_iterator.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah. sorry. you are right. I was having a different example in my head :-)

tool->print();
}
}

void rangeLoop(std::vector<Tool>& tools, const int beginning, const int ending){
auto tool = tools.begin(), end = tools.end();
std::advance(tool,beginning);
auto stop_point = std::distance(tools.begin(), end) - ending;

for( ; std::distance(tool, end) > stop_point; ++tool){
tool->print();
}
}
Expand All @@ -42,6 +52,8 @@ int main(){
indexLoop(tools);
std::cout << "Start iterator looping" << std::endl;
iteratorLoop(tools);
std::cout << "Start range looping" << std::endl;
rangeLoop(tools, 2, 4);

return 0;
}
4 changes: 3 additions & 1 deletion modernsyntax/lambda.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ void incrementCounter(int* counter, const unsigned int times){
int main(){
int counter(0);
// launch a thread to increment the counter
std::thread increment(incrementCounter,&counter,100000);

const unsigned int times = 100000;
std::thread increment([&times, &counter](){for (unsigned int i=0; i<times;++i){++counter;}});
increment.join();
std::cout << counter << std::endl;
return 0;
Expand Down
4 changes: 2 additions & 2 deletions modernsyntax/range_looping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class Tool {
};


void rangeLoop(std::vector<Tool> tools){
for(auto tool : tools){
void rangeLoop(std::vector<Tool>& tools){
for(auto&& tool : tools){
tool.print();
}
}
Expand Down