C vs C++

Started by
9 comments, last by ArchMiffo 21 years, 7 months ago
Ok, here''s the deal: I''ve been talking to a friend of mine and we got into a discussion about C and C++. He thinks that C is a faster and more effective than C++, I say that''s bullshit. What I want from you guys is some more input in the debate, am I totaly wrong? Are my friend wrong, or is there no real answer? My friend also thinks that shifting bits around will make a difference in speed (at least in a big program), which I also think is kind of wrong considering todays computers and compilers. So, give me some feedback so I know if I should take back what I said or convince my friend that I realy have a point.
Advertisement
If your friend is an expert in C/C++ then I am sure he will be able to tell you how to achieve C++ features in C, and then you will be able to make a sensible decision in the speed/code size tradeoffs associated with each C++ language feature.

If he doesn''t know any C++ at all, or doesn''t know how to use C to achieve OO-like constructs, he is clearly talking about things he has no knowledge of. For example, C++''s std::sort on built-in types outperforms the C qsort due to templates and the built in types operator< .

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley
"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley
Well, the fact is that on todays comps, it makes close to no difference at all, provided that you''re a decent C++ coder. But then again, if you were a bad C coder, it would run slow to.

Basically C++ is better because the OO design makes handling large code much easier(through encapsulation, of course).

But all in all its preference, as usual.


"With my feet upon the ground I lose myself between the sounds and open wide to suck it in, I feel it move across my skin. I''m reaching up and reaching out. I''m reaching for the random or what ever will bewilder me, what ever will bewilder me. And following our will and wind we may just go where no one''s been. We''ll ride the spiral to the end and may just go where no one''s been." - Maynard James Keenan [TheBlackJester ]
[Wildfire Studios ]

"With my feet upon the ground I lose myself between the sounds and open wide to suck it in, I feel it move across my skin. I'm reaching up and reaching out. I'm reaching for the random or what ever will bewilder me, what ever will bewilder me. And following our will and wind we may just go where no one's been. We'll ride the spiral to the end and may just go where no one's been." - Maynard James Keenan Name: [email=darkswordtbj@hotmail.com]TheBlackJester[/email]Team: Wildfire Games
Projects O A.D.The Last Alliance

He doesn''t know much C++ but I think he is refering more to the low level stuff, like shifting bits as I mentioned earlier. Another strong argument he uses is portability. Of that I don''t know very much. Could anyone enlighten me? Is C more portable than C++? And if so, why? Becouse it''s older?
Yawn...
Why are we debating C versus C++? Everyone knows you should use Intercal for everything!

"When you know the LORD you have no need for masturbation!"
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
We can all see where this is going.

"My dad can beat up your dad", etc etc.

Helpful links:
How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions
C++ is just an extension of functionality ontop of C.

Shifting bits for example, if you don't use any C++ built in types (is there special C++ bit shifting commands?), you can still go back to the C bit shifting. Then use the other features from C++ that you like.

Personally, the ONLY feature of C++ that I use is the classes to make the code more readable. I never use the std or anything else.

But its not because of speed, when I see std code, I choke, because it looks very sloppy to me... having [std::list]variable type stuff around everything.

But thats the nice thing. I'm not forced to use it... I can pick and choose what I want from the C++ functionality.


[edited by - joeyblow2 on September 2, 2002 12:13:03 PM]

[edited by - joeyblow2 on September 2, 2002 12:13:31 PM]
firstly it's not a competition

secondly c and c++ are language specifications. they don't specify how fast they are. they're just writing on a page. efficiency of code - that's down to the compiler. the fact that there are many compilers means that your question/debate doesn't really make much sense.

thirdly there are many commonly used library implementations with different strengths and weaknesses. some speed issues will be related to them.

fourthly there is programming ability. if you can't code well then you'll end up with bad code. c++ has features to help you code in an object oriented manner, in a generic way and procedurally. it's possible to abuse all of these.

fifthly most things you can do in c you can do in c++ if you so wish (see fourthly above)

if you're so really really bothered about speed and you can tell the difference between c and c++ then use assembly.

[edited by - petewood on September 2, 2002 1:56:04 PM]
quote:Original post by JoeyBlow2
Shifting bits for example, if you don''t use any C++ built in types (is there special C++ bit shifting commands?), you can still go back to the C bit shifting. Then use the other features from C++ that you like.


What are you on about? >> right shift, << left shift. In the C++ spec as well as the C spec. Whats the point in changing one of the core operators (ie, lets use #@ instead of +) that is ingrained into the language. The only issue I do have with C++ is the abuse of the "Don''t change operator meaning" rule when defining the bit shift operator for input/output streams. Seems a bit daft IMHO.

quote:
Personally,... I never use the std or anything else.

You should start learning it then.

quote:
But its not because of speed, when I see std code, I choke, because it looks very sloppy to me... having [std::list]variable type stuff around everything..


No excuse, use it in cpp files with using namespace std; (although I think that is extremely sloppy). Using stl takes a bit of getting used too, but speeds up productivity way more than classes do. It''s easy to use, quick (if used correctly) and tested by a very large population who tend to agree that it''s pretty cool. There''s also the algorithm routines (such as std::for_each) that have a huge impact on the readability and speed of your code. Start learning stl!!!


Anyhow, to answer the original question, arguing between C & C++ is a fairly pointless debate, consider the following :


  /* In C */typedef struct {    float data;} Thing;void SetData(Thing *t,float d){    t->data = d;}// In C++class Thing{public:    void SetData(float d) {        data = d;    }    float data;};// the above C++ code will be compiled to the equivilent of :struct Thing{    float data;};void SetData(Thing* this,float d){    this->data = d;}  


At the simplest level there will be no overhead. The only real place where you do get a (very small) performance difference is when you use a virtual function. This is because in order to make the function call you have to go via a pointer (stored in the vtable) to the actual function itself.

However there are ways around it using templates and compile-time polymorphism (as apposed to runtime with virtual functions).

This is the actual difference between say iostream.h (which you should not be using due to the fact it''s old and uses vtables) and iostream (oh no, std::cout !! Dam that stl... ) which is heavily templated.

People always argue that C++ is slow, OK, in the past that had a ring of truth to it, but now there is actually very little difference unless you are using really bad programming techniques, but the same could be said of any language.

Oh, and the other largely quoted argument goes something like this :

Using operators generates temporaries which is really bad and slows performance. (ie, c = a+b; the operator for + generates a temporary which is bad). I have to say, looking through the ASM listing that a decent compiler produces disproves this because nowhere is there a temp generated. Compilers these days aren''t stupid, they can usually figure stuff like that out, so wjhy not use it?

This topic is closed to new replies.

Advertisement