When programming in C, there are simply too many thing that one needs to track down in terms of allocating and freeing resources in appropriate timing and space. Consider the very simple example below, which takes in user's input words and saves them.
Because one does not know how many words will be there to save, one has to create an array of struct Word dynamically by calling malloc(), assign appropriate memory space for each word pointer in the struct, and finally call free() on all dynamically allocated resources when exiting. It is the programmer's role to take care of resources, and when the program grows big and complicated, one becomes more and more prone to mistakes and bugs.
Fortunately enough, there is C++ to help. Although many people, including Linus Torvalds, complain about C++, I happen to believe that C++ is a good choice for some applications, not all. There is a catch though. Somebody must do the hard work of taking care of all the memory allocation of classes.
Consider the C++ version of the same code.
Here, I am intentionally using char* and not string object to demonstrate how C++ class constructors and destructors can be designed to relieve the burden of memory allocation from the user. Note that in main() function, the programmer never has to call malloc() or free() to allocate resources manually. All these are performed by the constructors and destructors, so the programmer can simply create and make use of class Word as if it is a primitive data. To focus on resource management, I an using as much as C features as possible, except for object-oriented structure and C++'s vector class. The vector class allows one to assign memory dynamically without calling malloc() or free(), as vector's constructor, destructor, and methods will take care of all those.
It is now clear that the burden of memory management now lies in the hand of the class and library writer. If the class or library is not well written, the entire program will become unstable. From the perspective of a programmer who simply makes use of the class and library, it is apparent that writing programs is much simpler, given that the library is well-designed.
It is now clear that the burden of memory management now lies in the hand of the class and library writer. If the class or library is not well written, the entire program will become unstable. From the perspective of a programmer who simply makes use of the class and library, it is apparent that writing programs is much simpler, given that the library is well-designed.
In a follow up post, I will go through the class design rule in C++, namely the rule of three. In fact, the above C++ example is an excellent case that needs to be modified to comply the design rule.
No comments:
Post a Comment