From C To C++

I have advised every new programmer to at least understand certain concepts in C. Howmuchever higher paradigm we use to program, understanding of the basic memory management and basic data-structures go a long way. C is one of the best languages to do so, it carries the least baggage in this regard. Of course, many of us have moved to C++ for various reasons. It is not that C++ is always better than C, but for some problems C++ makes it easier to write maintainable and flexible code; or achieve the desired balance in an easier way.

Moving to C++ is not easy, there is a shift in paradigm in thinking. Nobody can escape understanding OOP, though OOP can be applied equally well in C. The only difference is that C does not support it in the language.

Unfortunately I have seen a lot of C++ code which ends being a mess of C and C++. This maybe either because of programmers who either overdo or underdo the paradigm shift or because of reamins of legacy code. The difference is not only in using classes, the advantage comes out of using the standard library facilites instead of doing the low-level manipulations yourself. While there cannot be any such list which can guide programmers, here is a small list which can help you see some benefits by coding in C++.

  • Use vectors instead of arrays. A vector knows its size, an array does not. You can also resize a vector without doing explicit memory allocations.
  • Use inline functions instead of macros. Macros do not obey the C++ type and scope rules. You can also use templates for more complex macros.
  • Use C++ string class instead of raw C string. Again, this can save you from many explicit memory manipulations and provide a lot of string utilities as well.
  • Use new/delete instead of malloc/free. While both end up allocating memory, new also invokes the constructor to build the object. Using malloc/free objects will involve a lot more work from your side. There are other differences like, you need to calculate the memory size to be allocated before using malloc, and since malloc only allocates memory it does not know type of the data requiring you to type case to use it. There is no harm in using malloc for basic data types, as they are not objects. But it is a cardinal sin to cross the allocation and deallocation techniques. new/free and malloc/delete is nothing but a disaster.
  • Use cin/cout/cerr. The input/output stream objects are typesafe and extensible.
  • Use libsigc++ for callbacks. A couple of benefits is that it provides typesafety and adaptors for connecting dissimilar callbacks.

Similarly, Boost provides some excellent libraries which you can use to work at a higher abstraction. C++ aims at eliminating some of the pain points in C. However, it is critical to unlearn some of the C ways of doing things and adopt the C++ ways. There are popular arguments that these abstractions cause slower programs, which definitely might be true in some cases. However, I wonder if majority of applications see this.

Another argument is about the bloat that the C++ programming style brings in. While it can be blamed on the language, an improper use is the underlying cause most of the times. There are some guidelines while thinking of the design too, but let us keep that for another post.

Do not take these points as a list for why C++ is better than C, and feel free to add to this. I think the suitability depends on the context, not only on the features. However, if you do end up using C++ try to follow these. If you do not, you will not see any advantage of using C++; if you do, you will see and enjoy the C++ goodness.

Discussion [Participate or Link]

  1. indyank said:

    C defintely has relevance even today…mainly as a systems implementation language…C should be learnt before one steps into java or C++….otherwise one may not be able to appreciate the differences between procedural and OOPS languages…

Say your thought!

If you want to use HTML you can use these tags: <a>, <em>, <strong>, <abbr>, <code>, <blockquote>. Closing the tags will be appreciated as this site uses valid XHTML.



Abhijit Nadgouda
iface Consulting
+91 9819820312
My bookmarks


This is the weblog of Abhijit Nadgouda where he writes down his thoughts on software development and related topics. You are invited to subscribe to the feed to stay updated or check out more subscription options. Or you can choose to browse by one of the topics.