Programming 101 1


When I was an undergraduate studying programming for the first time we were taught C++ a fast, efficient language that introduced me to Object Orientated Programming (OOP) and the inevitable:

  • error LNK2019: unresolved external symbol

Which maintained to be the bane of my existence for a long time! It wasn’t long before I discovered C# and the abilities of managed code and rapid-prototyping GUI based applications. The allure of managed code are the simple things like garbage collection. When you are trying to track down a double freeing seg fault error for a few hours garbage collection seems like an absolute dream.

Needless to say after nearly 15 years coming back to C++ was a bit daunting but  for those of you who don’t already know, I am happy to report C++ has come a long way since the year 2000… e.g. strings are no longer confined to the limitations of arrays of characters but have an implementation as part of the std namespace. Vectors (also part of the std namespace) allow resizing arrays which iterator pointers and all manner of search, re-order and printing functions built in all in all making life so much easier.

std::string* str = new std::string("here is a string non null character terminated");
delete str;

Simple enough and allows for easy declaration and clearing, as the string pointer is declared on the heap it needs to be deleted when we are through with it.. simple. Converting between char* and strings is pretty straightforward as well.

char* chararray = "testing testing";
std::string* str = std::string(chararray);
delete chararray;
delete str;

But things can get pretty interesting and lead to memory leaks if you don’t keep in mind the difference between the pointers… here is an example:

std::vector<std::string*> theStrings;
std::string* str = std::string("first string");
std::string* str1 = std::string("second string");
//push some elements into the vector
theStrings.push_back(str);
theStrings.push_back(str1);
//this is something I would have never previously done in c++
theStrings.push_back(new std::string("third string inline declaration!"));
//Remove the first element of the vector
theStrings.erase(theStrings.begin());
//Clear the vector completely
theStrings.clear();

Now while at first glance the erase and clear functions should do exactly what you what (i.e. remove the specific elements from the vector and free the memory) actually in the above case because the vector is holding a set of pointers to objects when they are individually erased and the vector cleared the memory is not freed! I learned this the hard way! It is appropriate and necessary to do the following:

 

std::vector<std::string*> theStrings;
std::string* str = std::string("first string");
std::string* str1 = std::string("second string");
//push some elements into the vector
theStrings.push_back(str);
theStrings.push_back(str1);
//this is something I would have never previously done in c++
theStrings.push_back(new std::string("third string in-line declaration!"));
for (int cnt=0; cnt<theStrings.size(); cnt++)
{
          delete theStrings[cnt];
}
theStrings.clear();

This can of course be handled more elegantly in a situation where a vector is required, this is just an example! With any luck I will be sharing more of these pearls of wisdom as I progress my own re-learning of C++.


Leave a comment

Your email address will not be published. Required fields are marked *

One thought on “Programming 101