C++ for C programmers, part 1 of 2

As Stroustrup said, there’s no such programming language as C/C++. C and C++ are two different beasts.

I’m not going to argue here about which is better. For small or embedded projects, the complexities of C++ can get in the way. For larger projects, some of the features of C++ are very useful.

I’m simply going to list the features C++ adds to C, from an ex-C programmer’s point of view (yes, I was one of those people who believed that C++ was just “C with classes”). The idea is to follow the links to further information if you want to know more about any of them.

This is part 1 of 2, the non-OO features:

Please send your feedback, and let me know if I’ve missed any non-OO features. The following week’s entry contains the second part describing the object-oriented features C++ has added.

3 May 2010 by Ben    14 comments

14 comments and pings (oldest first)

Fabien 3 May 2010, 23:50 link

You forgot the most important:

Don’t use malloc, free, new[], delete[], or even delete.

Do use std::string, std::vector<>, and smart pointers.

Loup Vaillant 4 May 2010, 00:37 link

Talking about gotchas, I prefer the FQA over the FAQ. Quite depressing, but more entertaining.

Jason Baker 4 May 2010, 00:47 link

You might want to consider calling this part “1 of 2”. I was trying to figure out exactly what part one half meant. :-)

Hi !

The tutorial is excellent and is now part of my bookmarks, under the heading of useful tips.

However, i can’t let a comment pass uncommented. :)

The advice of not using new, new[] and their associates delete and delete[] is rather questionable.

C and C++ share the same philosophy of supposing a programmer aware of the risks. The languages usually trade extreme security for extreme performance.

And while malloc()/calloc() and free() should be generally avoided in terms of orthodox C++ programming, sometimes their use is recommended. For instance, when you got to pass a pointer to a C library.

Kevin Bowling 4 May 2010, 07:08 link

Looking forward to part 2.

[…] Chris Lomont’s Publications page Toshi’s Project Page be nice to your cache bit twiddling hacks A Whirlwind Tutorial on Creating Really Teensy ELF Executables for Linux creating small win32 executables techniques for reducing executable size tiny pe references on coding for optimization on x86 architectures the impossibly fast c++ delegates member function pointers and the fastest possible c++ delegates const illustration in c++ rant on c++’s operator new optimizing c++ (pdf) c++ and the linker hidden features of c++ source code for data structures and algorithm analysis in c c++ for c programmers […]

[…] adds to C, from an ex-C programmer’s point of view. A couple of months ago I wrote part 1, which detailed the non-OO features. This second part details the object-oriented features, though I haven’t given them exactly […]

caf 10 Aug 2010, 17:44 link

You might want to mention that `inline’ is also a co-feature with C99.

coder 26 Aug 2010, 05:46 link

Thanks for posting this, I have this book, but a lot of things have changed since this book has been published. Thanks again for posting this! :-)

Coder 23 Nov 2010, 06:35 link

Thanks for posting this information, very helpful when comparing C and C++, thanks again! :-)

[…] C++ for C programmers, part 1 of 2 — the non-object oriented features […]

Geoffrey Hunter 23 Oct 2012, 11:20 link

I would be interested to know why malloc is ‘acceptable’ in C yet frowned upon in C++.

Ben 23 Oct 2012, 11:45 link

@Geoffrey: in embedded systems, you often don’t use either malloc or free (heap allocation can be dangerous). But to answer your question more generally, here’s a StackOverflow answer on that — basically new is usually better because it’s type safe and calls your constructors.

Sayeed Sezan 28 Nov 2012, 08:36 link

Your post so is informative. I like C and C++ programming. thanks