Advertisement

C or C++?

Started by April 11, 2001 02:38 PM
75 comments, last by Chronoslade 23 years, 10 months ago
quote:
Original post by Chronoslade

Eventhough I am not an expert, I must say its going to be the programmer that creates the bloatware. If you take the time and completely plan out your idea before you try to code it, you should come out with a pretty good code. A lot of programmers(and what i used to do) would be make a very basic design and figure out the rest as you go. This can cause frustration and and sometimes useless code. Take the time, plan it.....just because a program has bloated code dont blame the language, it just does what you tell it, blame the programmer that wrote it.




Chronoslade, I agree completely. I think this is why some people find it easier to program in C over C++ (or rather procedural programming over object oriented programming). The compulsion to just jump in and start coding a new project is great. Most people don''t take the time to properly plan and design the program before putting finger to keyboard (to use the updated "pen to paper" term). In C, you can get by with this hack-n-code style, but you will end up burning yourself if you do this in OOP.


- Houdini
- Houdini
IMHO another reason why people think C++ programs are bloated is that you always end up wrapping C code, and thus are limited in your design or need ugly hacks to implement it, instead of just using a nice clean idea from the start. Compare MFC to the BeOS API...
Advertisement
That is true, I did speak of C++= refering to OOP. But, most of the features of C++ are just to support OOP.

The C++ Programming Language, by Bjarne Stroustrop

Early applications tended to have a strong systems programming flavor. For example, several major operating systems have been written in C++ [Campbell,1987] [Rozier,1988] [Hamilton,1993] [Berg,1995] [Parrington,1995] and many more have key parts done in C++. I considered uncomprising low-level efficiency essential for C++. This allows us to use C++ to write device drivers and other software that rely on direct manipulation of hardware under real-time constraints. In such code, predictability of performance is at least as important as raw speed. Often, so is compactness of the resulting system. C++ was designed so that every language feature is usable in code under severe time and space constraints [Stroustrop,1994$4.5].



--------------------------------------------------------------------------------


The idea that C++ is only for high-level apps is absurd.

------------------------------------------------------------

Low level efficiency? Most low level efficiency I''ve gotten out of C++ is basically C style code. So, yes, you can accieve low level efficiency in C++, using a diferent type of coding style. I''ve found it much easier to code low level in C, although I''d love for you to show me an OS coded entirely in C++(by that I would mean OOP) rather than one in C. Of course, if you compile as C++, and you use little more than function overloading, the program will be as small and as efficient as C code, but by C++ I''m talking about the OOP principle behind it and so on.

You can do OOP in C, but most people refer to it as structural. So, I''m saying, show me an OS as efficient as Linux(Unix, BSD, whatever), and as small, and that has the same features, and I''ll take back everything I said. Of course, Linux has been worked on for years by thousands of people, but surely something to the nature of proving C++ is good for low level stuff will prove to me that C++ is decent at it.

This is not a flame, I also use C++, I just use it for higher level apps.

But of course, the programmer is to blame for alot of the bloatware. I''ve seen(and made) bloatware myself in C, its not pretty, but I''m saying, its hard not to make bloatware in C++ for low level programs(using OOP).

-----------------------------

A wise man once said "A person with half a clue is more dangerous than a person with or without one."

The Micro$haft BSOD T-Shirt
-----------------------------A wise man once said "A person with half a clue is more dangerous than a person with or without one."The Micro$haft BSOD T-Shirt
C++ can be be very fast and efficient if used properly, and bloated and slow if not used properly. A good read is "Effecient C++ Performance Programming Techniques" by Dov Bulka and David Mayhew.

It takes a while to really get used to C++. I use all of its constructs (classes, templates,...), but make calls to the C runtime library. I find that using C++ aids heavily in large projects and code reuability. Say for example you need a dozen different sets of data each in a linked list. Rather than writing separate code for each data type, you can use a single template class for all linked lists. This is one of the clear advantages C++ has over straight C. Also, if you make a mistake in your linked list template (or whatever else you''re using), you only have to fix the mistake once.

I admit that I was reluctant to learn and use C++ at first, but now I''m glad that I did. I love it and recommend it. I find it MUCH easier to design and write large projects with it. Ok, I''ll quit rambling now...
quote:
Original post by _rpg_guy

Say for example you need a dozen different sets of data each in a linked list. Rather than writing separate code for each data type, you can use a single template class for all linked lists. This is one of the clear advantages C++ has over straight C. Also, if you make a mistake in your linked list template (or whatever else you''re using), you only have to fix the mistake once.




Is a linked list using pointers out of the question ?
You can convert small values to pointers and for large datastruct you normally don''t want to store all the data in the list at least not for very dynamic lists. You don''t get any typechecking but anyway. I think templates are a great construct but people tend to forget that the datacopying can be rather heavy for big data. Maybe list is not the best example but there are other datastructures where you really shouldn''t store the data itself, only a pointer to it.



First off, I have to say I much prefer C++, but there''s a fine line between the two. I don''t use classes and inheritance for everything, to do so would be foolish (in my opinion). Fortunately, combining the two languages is easy, and highly advantageous. Mainly because what it comes down to is they''re the same language.

People get so riled about this question, and I really don''t understand why. There are advantages and disadvantages to programming in either style. I say ''style'' because that''s really what it comes down to. You can write the same program in either C or C++, and it will work exactly the same way, be exactly the same size, execute just as fast. It all comes down to the skill of the programmer and how well he understands the techniques.

C++ allows a more true-to-life model of how things are associated with each other. It can significantly decrease the amount and complexity of the code you have to write to perform a task. Yet one must be fully aware of what''s happening behind the scenes when you work with ANYTHING that''s that high-level. C++ can add a lot of overhead that your program doesn''t need. However, C can be abused just as much as C++, if not quite as easily.

I would recommend to anyone: learn C++ first, and learn it well. Find out exactly what operations are performed when the code is executed. It''s not harder to learn than C, and at any time you like you can switch over to strictly C without having to learn anything new. I say learn C++ first because it''s around 90% the same syntax as C, and it''s really useful to help you learn to visualize your program.

There are good, valid reasons to work in C++ for just about any program, but there will always be someone to contest what''s a good technique and what isn''t. Finding out for yourself is the best (and only really effective) way of determining the best language for your project.

My 2c.


____________________________________________________

"Two wrongs do not make a right; it usually takes 3 or more."


Resist Windows XP''s Invasive Production Activation Technology!

____________________________________________________
"Two wrongs do not make a right; it usually takes 3 or more."
Some mistakes are too much fun to only make once.
Never anger a dragon, for you are crunchy and you go well with brie.

Advertisement
quote:
Original post by kvh

IMHO another reason why people think C++ programs are bloated is that you always end up wrapping C code, and thus are limited in your design or need ugly hacks to implement it, instead of just using a nice clean idea from the start. Compare MFC to the BeOS API...






MFC isn't exactly the pinnacle of C++ Win32 code, but you have a good point. For example, exceptions are obviously more efficient that return values.


quote:
Original post by ImmaGNUman

That is true, I did speak of C++= refering to OOP. But, most of the features of C++ are just to support OOP.



Still wrong...if you think that C++ is basically only OOP, you must have very little understanding of the language.

Namespaces, explicit scope resolution, and function/operator overloading eliminate the need for notation. Exceptions eliminate the need for tons of conditional logic to achieve a graceful shutdown during exceptional conditions. Function and class templates support generic programming. Classes support modular as well as OOP and generic programming.

The C++ standard library provides algorithms, statically typed containers, increased support for portability,

etc.


quote:
Original post by ImmaGNUman

Low level efficiency? Most low level efficiency I've gotten out of C++ is basically C style code. So, yes, you can accieve low level efficiency in C++, using a diferent type of coding style. I've found it much easier to code low level in C, although I'd love for you to show me an OS coded entirely in C++(by that I would mean OOP) rather than one in C. Of course, if you compile as C++, and you use little more than function overloading, the program will be as small and as efficient as C code, but by C++ I'm talking about the OOP principle behind it and so on.



C++ supports four programming paradigms. Don't blame the language for the idiots who try to fit anything and everything into what they call an OOD.


quote:
Original post by ImmaGNUman

You can do OOP in C, but most people refer to it as structural. So, I'm saying, show me an OS as efficient as Linux(Unix, BSD, whatever), and as small, and that has the same features, and I'll take back everything I said. Of course, Linux has been worked on for years by thousands of people, but surely something to the nature of proving C++ is good for low level stuff will prove to me that C++ is decent at it.



1. A language does not a good programmer make.

2. Your acceptance of C's ability proves that C++ can handle low-level stuff.


quote:
Original post by ImmaGNUman

This is not a flame, I also use C++, I just use it for higher level apps.



The only valid reason for using C for some things and C++ for other things is personal preference, but there are plenty of good reasons to prefer C++ over C in all categories. I made this point earlier...


quote:
Original post by ImmaGNUman

But of course, the programmer is to blame for alot of the bloatware. I've seen(and made) bloatware myself in C, its not pretty, but I'm saying, its hard not to make bloatware in C++ for low level programs(using OOP).



OOP is suitable for mid- and high-level apps; however, it falls down on low-level apps because of its need for dynamic typing (read: run-time support). Modular, procedural, and generic are excellent for low-level apps when used correctly. You don't have to use OOP to solve every problem; you don't even have to stick with one paradigm for every portion of an app!


quote:
Original post by zel

Is a linked list using pointers out of the question ?
You can convert small values to pointers and for large datastruct you normally don't want to store all the data in the list at least not for very dynamic lists. You don't get any typechecking but anyway. I think templates are a great construct but people tend to forget that the datacopying can be rather heavy for big data. Maybe list is not the best example but there are other datastructures where you really shouldn't store the data itself, only a pointer to it.



A pointer is a type, so nothing prevents you from making a std::list&ltint*> just as well as a std::list&ltint&gt. However, in C you would have to use void pointers or preprocessor magic. Void pointers either lead to lots of casting or poor typing. The preprocessor magic alternative is also more error prone than templates, though less obviously so.


Edited by - null_pointer on April 18, 2001 6:28:37 PM
I have heard this said about C that it is faster than C++. Then I hear people say C++ is almost as fast as C. Really who cares about that when you got 1.5gig CPU''s out now. I don''t want to encourage sloppy coding but if a few cycles are all I give up for a language that has portability and modular coding and can do everything C can I say use C++. I don''t hate C but have started out learning C++ first and like it better from what I have seen of C. It’s all a matter of opinion in my opinion.

Windows 98/SE/ME SUCKS!
Deal with it!
if(windows crashes)
run Linux
else
yea right!!

Resist Windows XP''s Invasive Product Activation Technology!
Ah, another person using my banner, I''ll add you to my list (18 people I''ve seen so far). BTW: Templates are just really complex macros, I''ve faked them in C before using macros, it isn''t really hard. With compilers that don''t follow the C++ spec to well (MSVC), they''re treated almost exactly like macros. You can''t put their functions in .cpp files and have the template class in a header file in MSVC, which sucks... Not C++''s fault though.

"Finger to spiritual emptiness underlying everything." -- How a C manual referred to a "pointer to void." --Things People Said
Resist Windows XP''s Invasive Production Activation Technology!
http://druidgames.cjb.net/
quote:
Original post by Null and Void

BTW: Templates are just really complex macros, I''ve faked them in C before using macros, it isn''t really hard.



Including partial specialization and default parameters?

This topic is closed to new replies.

Advertisement