C vs C++ ?!

I’m not sure how a template can work out faster than a macro tho. If you can provide details of this, that would be cool. A little program with some timings in or something?

Um, macros expand their arguments each time they’re used. This can lead to HORRIBLY slow code. Here’s a trivial example where a template will be much faster:

#define square(a) (a*a)

template square(const T a )
{
return a * a;
}

float someReallyComplicatedFunction( )
{

}

int main( )
{
square(someReallyComplicatedFunction());
}

Now, how many times will someReallyComplicatedFunction() get called? Twice in the macro, and once in the templated function.

The templated function is also type-safe, as an added bonus.

Macro’s are evil for a whole variety of reasons, so don’t even get me started

– Zeno

Ok - so you decide to write your smallish project in C and it grows and grows and grows - I think C++ is better. Knowledgeable use of C++ (ie. know your compiler!) allows you to generate tight code anyhow - and you have the opporunity to try out different coding patterns - OOP really can be quite useful. (virtual functions are the most useful, ok a little slow but equivalent functionality in C is tough - without writing your own compiler

but if there isn’t any difference in speed or C is faster then C++ why did they make C++? is it only for classes and OOP stuff ? or is there anything they changed ?

McZ: i use C++ (and in it C), dont know why some guys think that both languages are different enought to say that one is faster, or produces smaller code, the difference is produced by the programmers! If you know that C++ is build on C you know that these languages are nearly identical, some stuff is added to C++ but you can write everything in C (ive never used RTTI so i dont know how to do it in C, but i can think of a struct with an union and a typeflag, or even faster if you use an array for each type and controll everything when it will be allocated) !!!

Some examples:

Templates in C (as a header file ‘c_template.h’):

#ifndef TYPE
#error “Type not defined”
#else
void TYPE myTemplateFunction(TYPE *var){
return *var;
};
#end if

usage:
#define TYPE long
#include “c_template.h”

and it will produce the same ammount of code like c++ templates will do (if both are inlined/not inlined)!!!

next example overloading operators like ‘+’ isnt more than writing a macro:

#define +calc(x) calc_plus(x)
long calc(long x);
long calc_plus(long x);

virtual functions:

struct X{
void (*my_virtual_func) (X *this, long);
} abc, def;

void test(X *this, long);
abc.my_virtual_func= &test;
void test2(X *this, long);
def.my_virtual_func= &test2;

dont tried to compile the code, has maybe some bugs, but thats the way it works !!!

and finaly the this pointer as example in C:

struct XYZ{long a;};
void GetSomething(XYZ this, long abc){
return abc
*(this).a;//maybe a syntax error produced by me, but ‘->’ is c++ i think ?!
};

so the difference is only in code size (if you write itentical code!) and not in speed or size of the executable!

my code examples are maybe more c++ than i think, but its only to show that you can write it in c…

finaly you can do your c++ compiler easier than you thing, only convert it to c code and then use a c compiler !

Compiler: C++ -> C -> ASM -> something the Linker understand

never used streams…

C++ can produce larger code, agreed… But I have yet to see a C++ code slower than a C code.

I have converted all my current project to Standard C++ with VS.NET, none of them are slower.
They’re actually faster, smaller (in code, but sometimes gives a larger exe) and most importantly: less buggier.

C++ isn’t an easy language, it’s been two years I’ve been actively programming in C++ and I’m still learning new stuff everyday.

From a theorical point of view, C is faster than C++, and ASM is faster than C…
But in reality, C++ offers a larger and easier control while still offering backward compatibility with C and ASM. So if it’s really slower, then you surely did something wrong.

To give a small and simple exemple of C++ being faster than C, while keeping things simple is sorting an array:
the STL sort() will never be slower than C’s qsort(), because it’s eliminating the overhead of the C function (yes, C’s overhead, not C++).

[This message has been edited by GPSnoopy (edited 04-23-2002).]

You can’t write object oriented code in c. C misses virtual functions.

As for speed, it’s up to the compiler. Some optimize better than others. As for one version of C being faster than another is not quite true because of this. The language is the language and nothing else.

As I recall, sorting has a running time of n log n so comparing programming languages based on a problem with a proven running time is not quite right either.

I do recall there may be more little more overhead with C++ and thus consuming some extra cpu cycles. In the end, it all boils down how the program performs (alogorithmically speaking, it’s running time).

Personally, I’m going to move to C++ so I can encapsulate thread safe functionality and build a framework in which all my future apps will use. I want to concentrate on working with the data I’m trying to represent.

If I wanted to jump right in and get familiar with OpenGL, I would go with what I know best, then learn the preferred language later. One thing at a time.

my two cents.

lobstah…

[This message has been edited by lobstah (edited 04-23-2002).]

[This message has been edited by lobstah (edited 04-23-2002).]

Cmon people, this has been discussed 1000000 times, and every time the same story: one says: c ist faster, the other says: no, this is a stupid rumour. And this will never end, its just the same as linux/windows holy war.
http://groups.google.com/groups?q=c%20vs%20c%2b%2b&hl=en&sa=N&tab=wg

-Lev

You’re in for trouble if you try and use plain C to construct a large project.
Believe me, I’ve worked for a company who’s product is a sales/warehouse management database program suite - it’s entirely written in Pro C (ie. C with embedded sql). It was laughable (but on occasion, it made me cry ).
Use c to produce little demos, if you will, but C++ is the way to go for anything bigger.

Originally posted by davepermen:

why using c for it if c++ does the same but with less coding work? ok, because you love c… but anyways…

It is because not all embedded systems has C++ complier.

I used to think much the same way as some others on this board did about C/C++. The truth is, C++ has much more than is initially obvious.

For example the obvious uses of templates include: writing typesafe macros without byzantine side effects, or making parameterized datatypes (like std::vector). However, it turns out you can use templates to force your compiler to do all sorts of copmile-time decisions that let you write rich, efficient library code. This is extremely useful when you work with other people in programming (even as few as 3 or so).

Template code is only instantiated when it’s used or explicitly instantiated. Also, it is easily possible to make templated classes to share code between different parameterized implementation using inheritence.

The nonobvious uses for templates is for generic programming and metaprogramming. Things like expression templates, STL, Boost, Loki, MTL, etc. etc. are simply magical to behold.

Problem with C++? Pretty big, actually. You need to be fairly committed, knowledgeable and just smart to avoid hanging yourself with the rope that they give you. For example, things like operator overloading can be used to greatly improve code readability, but it can be abused to achieve the opposite result.

Bottom line? You should educate yourself in C++. For almost every modern software engineering effort (including games, which are just getting huge) C++ is a better option than C. In the long run, you can do more with less.

-Won

PS To utterly blow your mind, read Modern C++ Design by Andrei Alexandrescu

I’m with Won.

I’ve prgrammed my first OpenGL app using straight C code and during development, it occurred to me I should be using C++ to help manage the complexity.

lobstah…

I’m up to three cents now!

I just love the C vs. C++ debate. I will have to post this again just for fun !!

I actually do use C++ once in a while. From the application level it is great for organization at a high level. The internal details and low level programming (fileio and math routines) will always be faster in C and asm. But i constantly mix C, C++ and asm. They all have there uses. Mix as much as you like. IF you are careful with your coding they play together quite nicely.

Devulon

Michael Steinberg

“You can’t write object oriented code in c. C misses virtual functions.”

OOP has nothing to do with virtual functions. That’s just the way C++ implements polymorphism. And there is more to oop than polymorphism. In fact the original definition of oop didn’t mention polymorphism.
There are plenty of projects that use C for oop.

I guess you knows alot more then I do about C and C++ difference

I use both C and C++ so far becouse it is easy to use for me… and I just love the class stuff and overloaded functions… it is easy to use and understand when you know how they work…

This is a debate about what you define OOP. Many faq’s and I believe even the inventor of C++ say that polymorphism is what OOP makes “practical” (?). Just having structs or simulating OO stuff with C is not object oriented programmin in my eyes.

Originally posted by Devulon:
But i constantly mix C, C++ and asm. They all have there uses. Mix as much as you like.
Devulon

nothing to add

[This message has been edited by ScottManDeath (edited 04-24-2002).]

Originally posted by Michael Steinberg:
This is a debate about what you define OOP. Many faq’s and I believe even the inventor of C++ say that polymorphism is what OOP makes “practical” (?). Just having structs or simulating OO stuff with C is not object oriented programmin in my eyes.

In case if it has anything to do with what I said earlier, I did not mean C is an OOP. I meant you can have your software design OO-based, disregard of what programming lang you use for implementation. It is not only true academically, not also true practically.

Originally posted by Michael Steinberg:
…Just having structs or simulating OO stuff with C is not object oriented programmin in my eyes.

You may code in C++, but that doesn’t mean that your code is object orientated. OOP comes about whenever the programmer chooses to use the technique, not due to what language you are using.

I was speaking in a matter of ability to write OOP programs. C misses so many features that one can’t reasonably write OOP programs, even if they have objects and stuff that isn’t yet OOP. C++ gives the ability, nothing more, to write reasonable programs in an OOP style. It doesnt force you to. If I find that link to the article of a pro again I’ll post it here.
Trying to simulate OOP with C is a pain in the ass and doesn’t have the same potential.
Inharitance, polymorphism and protected members are some key-features to execute the OOP paradigm in my eyes.