C vs C++ ?!

this is little of topic… but I still want to know which language you are using ?

every .tar file loader and 3ds file loader and .ase file loader I have found use C code… fprintf, fread and so on… thats C code right ?! why isn’t there anyone with C++ source ? using ifstream/ofstrem?

when you write a file loding code wich one do you use ? C or C++ (fread or fstream) or anything else…

and then what is the difference… is anyone faster to read or something like that ? or is it just what the person who write the code wants to use?

I use fstream to everything… it is easier to understand and to use… but it is hard to convert the code I found who use fprint stuff…

http://www.opengl.org/discussion_boards/ubb/Forum3/HTML/006199.html

I’ve started using streams lately, cos you can do cool things transparently, when you derive your own file stream operators for classes.

They are significantly slower than C libs for most implementations tho…

Nutty

Hi,
i think that this will start an old topic, C or C++. Personaly, i like C 'cos its much easier to read it (i think here on algorithms), and it’s much faster than C++ (many,acculay, as i know all rendering algos and methods (let say raytracing) using C).

But, as you probably know, C++ is better for larger projects, that dont require speed, and require maintaining.

PS.
some C stuff are more powerful than C++ (also much unsafety), for example printf and cout

Saying C is much faster than C++ is completely wrong. Certain things are slower, but provided you know how to write C++ code properly, they both run pretty much identically in speed.

Provided you have a decent compiler of course.

Nutty

The question shouldn’t be: what shall i use ‘C’ or ‘C++’ or whatever language…
But -> Why should i use C,C++ for?

:wink:

or read this for a complete briefing! ;)) http://www.orkysquad.org/main.php?xtra=Programming_rules

Is there any difference in C and C++ code layout? or is it the functions that are different ?

and ofcourse you can mix them and use both… but I wounder why do that if they do the same… if it is a big speed difference then I understand but if the speed is the same then it has to do with the programmers choice of language I guess.

but I think the C++ is easier to use with the class stream things… mostly I use it to read and write files becouse it is easy… I don’t get the fread thing to work the way I want to… but the fstream thing works perfectly.

The only real thing that makes C++ slower than C is the extra overhead that C++ code often produces. What I mean is that depending on the code C++ may have to make decisions about the type of object during run-time. Things such as templates tend to enlarge code. Generally the code inside of a C++ function is basically C code. Its just that in many situations there is extra overhead in getting into and out of the function. Largely in cases where constructors and deconstructors get called upon entering and leaving a function.

Please make not that this is a generalization. The speed of C++ versus the equivalent in C is largely related to exactly how the C++ code is written.

I personally use C. C++ in my opinion has way too many compatibility problems across platforms and compiliers. Generally if you avoid using features that cause incompatibilities I find you are right back to almost pure C code. So whats the point ?

Devulon

Hi,
“Saying C is much faster than C++ is completely wrong.”,
hm… like Devulon sais, and i must agree, C++ “features” take little bit more memory than pure C. Also, like you said, it depend on compiler, and how it implement C++ routines.
But, generaly, algos are written in C or in assm .

c++ is not slower for the same thing… but different implementations of stuff are slower… like the streams cout,cin,fistream,fostream (thats how they are named, not?)

and i always use fwrite fread for binary files and fprintf for textfiles (only logfiles, so no reading needed… guess for reading the fstreams could be cooler…)

and, if you don’t knkow the difference, how about learning it? books are out there everyhwere explaining c and c++

bah… you’re all living in the past. The last system I worked on that had crappy C++ support was the dreamcast. PS2, and GC, and Xbox, and PC all have great C++ support.

C++ may have to make decisions about the type of object during run-time

Thats correct if you use RTTI, but I dont, and I dont think many ppl do either, as you can still work around it, and you only ever need it if you use multiple inheritance, which IMHO if you’re using, you need to redesign your class hierachy.

I dont use templates much either, they do increase code size, but thats all.

C++ is the future, its fast, efficient, and easier to use on big projects. Get used to it.

Nutty

I prefer, for file IO, fread/fwrite for binary. They are much easier to use than the iostreams for binary files (and I tend to build up binary images in memory and blast them out to disk). For text, I like printf when I’m not using std::string, but I use the iostreams for file IO on text files.

Well I dont use stream for text, cos that is slow…

But building binary images on disk is not portable, as you can’t be sure of the alignment on another development system. Reading in low level ints, shorts, bytes, floats, and storing them yourself is portable.

Nutty

You all discuss about using different functions and features of C/C++, but you miss the really important point: C++ is object oriented, while C is procedural. It is a completely different way of designing software.

It doesn’t really matter what functions you use for IO, you will get (nearly) the same performace. But when you design a product object oriented you have to write it in C++ (or Java, …), otherwise you can use C.

Greetings
Overmind

Just to be an awkward bas, I’m going to disagree, and say you can write object oriented code in C.

You can even put your function prototypes in struct’s, to hide the functions from being global. You dont have constructors obviously, but I rarely use them anyway.

I don’t how know popular it is, there are commerical applications which are written in C but their designs are OO.
You may miss some of the features in C++, but there is nothing to prevent you from implementing your owned virtual function tables, C structs with function pointers as elements etc.

structs with functionpointers are not members… you’ll have to pass over to them the this-pointer anyways…

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

Hi
I dont mean to flame you all, but I just need to defend my

favorite language and its really great features

Thats correct if you use RTTI, but I dont, and I dont think

many ppl do either, as you can still work around it,

In fact RTTI is one of the most cool things in the OO

programing - the performance difference between writing virtual

function which returns something like class id and using

dynamic_cast is negledible and dynamic_cast is really far more

usefull.

and you only ever need it if you use multiple inheritance,

which IMHO if you’re using, you need to redesign your class

hierachy.

Multiple inheritance is more than great - using it with RTTI

gives you ways to split up large classes in more elegant

parts, to reuse code better, to make and check interfaces and to reduce compile times in large projects. However virtual multiple inheritance is in fact dangerous and should be used carefully.

I dont use templates much either, they do increase code size,

but thats all.

In fact they do it, but that it is not all
Using templates is superior to writing macros - it much more safe, easy to debug, and in the case of a good compiler, you have perfomance even better compared to C! Look at SGI STL. STGL and most ot the contemporary math libraries are writen with templates.

C++ is the future, its fast, efficient, and easier to use on big

projects. Get used to it.
Nutty

I completely agree with this

Where are the problems:
Bad implementations with many compilers - especialy on templates. exceptions are cool, but too slow. Changes in interfaces, basic classes imply long compile times(Java is very cool here). streams are very slow really implemented - use printf, fprintf and etc…

This is really very brief defence of C++ and its features - I can talk one night about this, if I had the time

Regards
Martin

Fair enuff.

Where I work, we prefer not to have the overhead of RTTI. And our hierachies are usually simple enuff not to need it.

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?

Originally posted by Nutty:
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?

It can reduce call overhead by inlining (non)template functions where needed (compiler decides or inline/__forceinline)- something which is not supported by C… In fact if you write everything with macros, then you are the faster, but then the code grows even more - not practical.
Also I read somewhere that inlining and macros should be used careful, since too large code actually may hurt the performance, so it is better if the compiler decides(which is only the non-macros case).
And templates offer much more than the macros - you know this

Regards
Martin