Jump to content
  • Advertisement
Sign in to follow this  
Ned_K

This is not a C vs. C++ thread...

This topic is 4568 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I don't want a battle on which language is better, C or C++. Rather, are there certain things that C does more effectively than C++? I am already aware of the advantages of C++ with object oriented programming and the like, though I am currently only programming in C for classes and my own practice. I know some people claim that C code can result in faster binaries and compiles but that's not really what I'm getting at and I also know that C++ people will take issue with that claim. I'm looking for other things, if there are any. Does C perhaps give a slightly lower level of control than C++? Or does C++ contain ALL (as in every last bit) of C's low level functionality? Any insights and even random opinions are appreciated.

Share this post


Link to post
Share on other sites
Advertisement
My random opinion is that cows are nicer the horses.

My not-so-random opinion is that all C programs should (iirc) run using C++, so C++ should have everything that C has.

Share this post


Link to post
Share on other sites
Quote:
Original post by Nice Coder
My random opinion is that cows are nicer the horses.

My not-so-random opinion is that all C programs should (iirc) run using C++, so C++ should have everything that C has.


C99 has features that make it incompatible with C++ though. And I thought there were a few others before C99 as well...

Share this post


Link to post
Share on other sites
This page outlines the differences between the two. Designated initializers and restricted pointers are a couple examples of features that C++ lacks.

Share this post


Link to post
Share on other sites
No, It's a picture of a C vs. C++ thread...

Ok, sorry.

IMO, C++ is more or less a superset of C. Some things - as listed earlier - aren't but those features are rarely used AFAIK and easily done in different ways.

That said, C++ is generally considered slower, mainly because of the bells and whistles that it has. Classes themselves don't add a particularly significant overhead [ie none in release mode], but if you have stuff like RTTI [real time type identification] then there's the overhead. Throwing exceptions has an overhead in every function that they throw to IIRC.

That said, asembly-wise, the code generated should be pretty similar. In some situations, C is faster, particularly when you're dealing with uber-tight calling loops on singleton-pattern objects... something like:

for (int i=0; i<this->indexes; i++)
{
this->pushVertex(this->index);
}

Mainly because you know that there's only going to be one this pointer, and you can store your data one dereference closer to the application. In C++, the compiler has to actually send in a this*. And dereference this->index.

In C, this process would basically be:
if i>=indices jump out
call pushVertex(index+i)
jump back to start

In C++, this process would be
if i>=&obj->indices jump out
call pushVertex(&obj, &obj->index+i)
jump back to start

This is where C is naturally faster than C++.

As far as I understand it, speedwise, if you don't use any of the fancy features of C++, like classes, then the compilers will generate basically the same assembly. Which means the same speed. For me, it really comes down to convenience - I _like_ some of the features that C++ holds over C.

I think I'm done ranting. I hope everything here is true, but I'm no expert in C.

--CJM

Share this post


Link to post
Share on other sites
I like C. Its biggest advantage is that people can't go crazy with their code, i.e., develope a templated polymorphic RTTI multiple-inheritance class structure when some crafty C (or C-style) code does the same thing but far simpler, faster, and more robust.

But whenever I write in C I sorely find myself missing std::string and std::vector, and having to deal with pointer nonsense avoidable with references.

Share this post


Link to post
Share on other sites
Quote:
This is not a C vs. C++ thread...
Yeah right!

Now, don't think of pink elephants. Wait, I said ... oh never mind.

Share this post


Link to post
Share on other sites
Quote:
Original post by etothex
I like C. Its biggest advantage is that people can't go crazy with their code, i.e., develope a templated polymorphic RTTI multiple-inheritance class structure when some crafty C (or C-style) code does the same thing but far simpler, faster, and more robust.

Heh. You haven't seen die-hard C programmers and the macros they write.

Anyways, at this point, the primary advantage of C is that it is really easy to write a C compiler for an arbitrary given platform. Because of this, it's often the language of choice for embedded platforms.

(BTW: keep in mind that the page dcosborn posted refers specifically to C99, which is not widely implemented.)

Share this post


Link to post
Share on other sites
C also benefits of having a standardized ABI. Most languages are designed to interface with that ABI. Most libraries offer C bindings, regardless of what language they actually were written in.

This is why C++ has that extern "C" construct, after all.

Share this post


Link to post
Share on other sites
Quote:
Original post by Sneftel
Quote:
Original post by etothex
I like C. Its biggest advantage is that people can't go crazy with their code, i.e., develope a templated polymorphic RTTI multiple-inheritance class structure when some crafty C (or C-style) code does the same thing but far simpler, faster, and more robust.

Heh. You haven't seen die-hard C programmers and the macros they write.

Anyways, at this point, the primary advantage of C is that it is really easy to write a C compiler for an arbitrary given platform. Because of this, it's often the language of choice for embedded platforms.

(BTW: keep in mind that the page dcosborn posted refers specifically to C99, which is not widely implemented.)


LOL, I have seen some crazy macros. They're just as bad as the overkill C++ people.

But at least with macros you can preprocess it to something useful :) Also C99 roxxors and you don't even have to use macros...well...most of the time

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!