Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is C++ backward compatible with C ? Why isn't there some "pure" C++ language?

Tags:

c++

c

history

C and C++ are different languages, blababla we know that.

But if those language are different, why is it still possible to use function like malloc or free ? I'm sure there are all sort of dusty things C++ has because of C, but since C++ is another language, why not remove those things to make it a little less bloat and more clean and clear ?

Is it because it allows programmers to work without the OO model or because some compilers doesn't support high-level abstract features of C++ ?

like image 966
jokoon Avatar asked Jan 01 '11 18:01

jokoon


People also ask

Is C backward compatible?

C standards are not backward compatible - code written under the latest standard is not guaranteed to build with compilers that only support earlier versions.

Is C++ completely backwards compatible with C?

C++ is not fully backward compatible with C, wherever it's needed it has drawn a line.

Is all C code valid Objective-C?

"Objective-C is a superset of C" means that every valid C program is a valid Objective-C program (with the same meaning).

Why is C++ preferred over C?

Compared to C, C++ has significantly more libraries and functions to use. If you're working with complex software, C++ is a better fit because you have more libraries to rely on. Thinking practically, having knowledge of C++ is often a requirement for a variety of programming roles.


10 Answers

Because C++ would be right out dead if it wouldn't be compatible to C the way it is now. No one, except the fanbois, would like C++ if it wouldn't be compatible to C. (I know I'm probably going to be downvoted for this. Be it so!).

like image 136
Johannes Schaub - litb Avatar answered Oct 19 '22 22:10

Johannes Schaub - litb


About "Why there's no "pure" C++ language... Well, there is at least one. The most popular one is called D, it's great, well-designed, feature-rich, pleasant to code with, and you can use C libraries with it.

Ah, and almost nobody uses it. :)

The direct reason is that C++ is not bad enough to give people a good reason to port millions of lines of their legacy code to more modern, and as you described, "pure" languages like D.

like image 23
Kos Avatar answered Oct 19 '22 23:10

Kos


Most operating systems expose a C API, so if you want to use C++ for systems programming, you need some level of C interoperability.

That the C standard library was incorporated into the C++ standard library has historical and practical reasons: C++ began its life as an extension of C, and the C standard library was ready to use. It would be silly to exclude part of the library (like malloc and free) just because there are more idiomatic C++ alternatives: if you want to do stupid things, C++ gives you the power to do so.

For actual language semantics, the same applies - but to a lesser degree - and because of backwards-compatibility, C++ can never be entirely free of its C heritage.

like image 43
Christoph Avatar answered Oct 19 '22 22:10

Christoph


It's designed so that you can port C code and compile it as C++ code directly, and it allows for incremental upgrading of existing code. If C++ didn't have malloc/free, you couldn't compile existing C code as C++, because you'd have to pay some poor shmuck to go through and find all the malloc calls and replace them, which is expensive.

like image 21
Puppy Avatar answered Oct 19 '22 23:10

Puppy


C++ was designed to be compatible with C -- in fact it was originally a superset of C, but the C language has since changed to break that.

This means that C libraries -- including the C run-time library -- can be called from C++ code. It does not mean that it is a good idea to do so!

If you want a "pure" C++ then you can just use C++ without calling any C libraries.

[As others have said since I started typing this: The Design & Evolution of C++ is a good place to start reading for the background on this.]

like image 31
dajames Avatar answered Oct 19 '22 23:10

dajames


I suggest you take a look at The Design & Evolution of C++ to get a better feel for the reason the language turned out the way it is. There are historical reasons why C++ grew out of C and was made backward compatible with it.

like image 27
Brian Neal Avatar answered Oct 19 '22 23:10

Brian Neal


The early versions of C++ were built on top of C and in fact the compiler translated C++ code to C which was in turn compiled by the local C compiler. Bjarne Stroustrup is a great believer in backwards compatibility and would, I'm sure, resist any attempt to take functionality away.

You can read all about in in Bjarne's book The Design and Evolution of C++.

like image 24
David Heffernan Avatar answered Oct 20 '22 00:10

David Heffernan


There were plenty of more pure languages. They didn't get widely used, though, because they were too far outside the comfort range of most programmers. C++, on the other hand, allowed programmers to slowly ramp up by allowing C styles.

What you're doing is looking at languages like C# or Python and wondering why C++ doesn't look like them, but forgetting that getting there required stepping stones like C++ and Java, or Awk and Perl.

To adapt a quotation I heard earlier: C# is Microsoft's version of Sun's for-idiots version of Bell's version of C-enhanced-by-Simula.

like image 22
me22 Avatar answered Oct 20 '22 00:10

me22


All are right. To sum up: the reason is politics. If you want something to be popular, enhance something already popular and you have a ready market. Design something new and no one will be interested unless you are Sun, design some utter crap, but throw billions of dollars into library development and marketing.

like image 21
Yttrill Avatar answered Oct 19 '22 22:10

Yttrill


malloc() and free() are required so that you can call into C language libraries from C++ code. The C language library might return a pointer to memory allocated with malloc() that must be freed by the caller with free(); or, less commonly, it might require a pointer to memory allocated with malloc() that it can internally reallocate with realloc() or free with free().

like image 32
caf Avatar answered Oct 19 '22 23:10

caf