🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Linus Torvalds and C++

Started by
149 comments, last by Anon Mike 16 years, 5 months ago
Quote: Original post by Iftah
Not that one is slower or faster, that would take more specific context, just that one is more visible and for that no context is needed to notice it. Read my original text (which you quoted) I talked about easier to find, not about slow/fast. Again, it is harder to find a temporary variable than a visible variable. It is usually harder to notice a copy constructor than a memcpy.


There are also hidden 'copy constructors' in C:
struct bla {   int a, b;   double v[10000];   };void func(void){   struct bla x;   struct bla y;   // ... many hundred lines down ...   x = y;}

So, what was your point about better visibility and hidden overhead again ?
Advertisement
jeesh, I'm sure "glad" to see this is turning into another one of those "C is bad use C++" topics (you can replace "C" and "C++" with whatever languages/keywords/religions you prefer)...

Come on guys, the original post was about something else.

Quote:
Anyway, I wonder, why are there people who need C? Maybe it is because Linus Torvalds has something against C++? I looked that up in Google, and found this:


To answer the second question first: NO, it's not because of that. It will never be about what one person things, no matter who he/she is (and besides, Linus is a geek, not a god).

There are people who need C because they have to do "maintenance", there are still people who maintain accounting software written in cobol (met one of those 3 years ago, she was a nice 55 year old lady who never heard of OOP and I'm not kidding), there are also people who like to program something ELSE (like microcontrollers instead of games) and even if C is not "as good as C++" it's a lot easier to use C instead of (Z80 or whatever) assembly.
About Linus... well, his a jerk. To make things clear: Jerk = person with a HUGE ego. Wouldn't your ego be the same size if you were in his shoes? I've read the "Linus bashes C++" link, actually nothing in there surprises me. He's still a jerk, he is protecting (mainly) his 'precious' git and seems pissed off that someone made fun of his work by proposing a "better language, like C++" and he is right about some points (not 100%, but his ego doesn't let him learn that you can be both right and wrong at the same time :D)

Quote:
Why is C++ so hated in a lot of the open source world?


Excuse me, who said C++ is hated in the open source world? Just because older (call it "legacy") projects exist in C, doesn't mean they're the only ones. How about the squid proxy? What do you know, they migrated from C to C++ in version 3.0 (from reading their mailing list, looks like they did it because it was written mostly in object-oriented C anyway so C++ was the logical next step)...
IMHO, C++ is NOT hated in the open source world. Looks more like "C is hated in the gamedev.net world", but I don't get why is that... It's just A LANGUAGE. Like java/perl/python/php/your_own_favourite/your_own_grammar/assembly/whatever ! Nothing different.

And yeah, it has goto's. Big deal. The thread title says "torvalds and c++", not "please don't ever use goto".

So, to actually get to the part where I write an answer for the OP (I was meaning to get there):

Thread title: some people don't like apples and they try to avoid eating apples, other people love apples and eat them all the time. Torvalds just doesn't like C++ (maybe something happened between them when they were both young and foolish, I don't know), just leave it to that.

Quote: I've got some code on the internet, which are C++ libraries, and sometimes it happens that I get an email from someone who asks if it's possible to have a C version.


I guess that someone either only knows only C and doesn't feel like learning C++ or he's constrained by the circumstances to code in C.
You just do as you want: you can rewrite your libs ('convert' sounds bad :D) in C, tell them you can provide a C wrapper to those libs or just say "sorry, C++ is my language of choice"...

They're your libs, do as YOU like.

[joke]
I'm tired of how many people tried to tell me carrots taste good when they're boiled, I can't stand how they taste and one of these days I'll write an ugly post about carrots hoping that people might think "hey, why aren't some people eating carrots? is it because cobru wrote this post: link" ... :D
[/joke]

Have fun!
Q: How many programmers does it take to write a nice piece of software?A: MORE.
Quote: The big difference is that you know full well when you activate a function, but its harder knowing about operator overloading, hidden constructors, polymorphic methods and invisible variables.

Yes, there are more things C++ coders need to keep in mind. (witness the abundance of C++ pitfalls books)

However, are not macros (as frequently seen in older C libraries) worse? Slogging through code that makes heavy use of them (bonus for not capitalizing) has been much more of an annoyance for me than hidden copy ctors ever were -- not to mention the silly j*5 example.
E8 17 00 42 CE DC D2 DC E4 EA C4 40 CA DA C2 D8 CC 40 CA D0 E8 40E0 CA CA 96 5B B0 16 50 D7 D4 02 B2 02 86 E2 CD 21 58 48 79 F2 C3
Quote:
IMHO, C++ is NOT hated in the open source world. Looks more like "C is hated in the gamedev.net world", but I don't get why is that... It's just A LANGUAGE. Like java/perl/python/php/your_own_favourite/your_own_grammar/assembly/whatever ! Nothing different.


I don't completely agree with the 'it's just a language!' moto. What does it mean? Not all languages are the same. Sure, they're "just" tools. But tools are important. There exist bad or outdate tools. I think there are good, bad, and mediocre languages. C isn't bad, it's kind of a small and compact higher-level assembler. But, if we forget about compiler problems, C++ seems just superior in my eyes(and I don't even like it). And Python seem far far superior to both. Not for all jobs(mainly due to runtime performance of the current implementations), but for the vast majority of them. It is still "just" a tool. A much better tool.

Most of the reasons Torvalds list just doesn't seem right to me. He could just say 'C is the language I know and trust, and that's what I use. So either put up or shut up'. I can also perfectly understand the fact that there are compiler issues. But the rest? SC++L and Boost are total and utter crap? Why? You can still access the low-level constructrs, but only when you need to. C++ is used by a lot of substandard programmers? Maybe so, but so is C and every other language in existence. Abstractions are unnecessary? Everything has to be done at the 'raw byte memory level'? Everything? Everything? What does that even mean? It is possible for a working program to even exist without abstractions? Without some kind of abstractions, a design, a way to accurately describe and reason about it without mentioning low-level details, it's just a bunch of unrelated functions that fiddle with bits. It's like saying 'C++ makes your design apparent, so it also makes your design flaws apparent. In C there is design, but since all you can see is just hacks upon hacks upon hacks, hardly anyone ever notices'.
Quote: Original post by cobru
IMHO, C++ is NOT hated in the open source world. Looks more like "C is hated in the gamedev.net world", but I don't get why is that...


I think you'll find that the attitude is actually "sloppy reasoning is hated in the gamedev.net world". C has its place in programming. However, just because that fact is true that doesn't mean Argument A for using C is a legitimate argument. Sloppy reasoning also includes concluding that people saying Argument A is a crappy line of logic means that they think C doesn't have a place in programming.
Quote: Original post by BeanDog
Owned.

Really, Zahlman never ceases to give me reasons to leave him the hell alone when it comes to coding argumentsdiscussions.


To be fair, I initially wrote a much nastier version, then realized a couple of points of stupidity :) But if I'd had to do it from scratch, I probably would have come to the same result directly. ;)
One of the reasons for not using C++ in low-level programming is that exceptions do not work in kernel mode. I know this because the company I work for now actually does write a lot of our kernel code in C++ (which is unusual as I believe most companies write theirs in C, but we do provide C wrappers in our code). To get this to work, we have to maintain our own internal STL libraries that are exception-less among a few other changes.


C is better than C++ when it comes to performance; I don't think many people will argue with that. C++ can be better than C when it comes to ease of programming, although I've definitely experienced enormous headaches debugging my C++ code that I'm pretty sure I wouldn't have had to go through with C. I prefer writing code in C++ because I find it easier to make well-designed code than it is in C, and I hate having to spend so much time writing code C to deal with memory management, strings, etc. that I feel C++ helps make much easier.


I don't think Linus is correct that C++ is a horrible language and everyone should write in C. Whether you choose to use C, C++, or another language entirely depends on the scope of the application which you are writing. There's no silver bullet language, and that's a fact.

Hero of Allacrost - A free, open-source 2D RPG in development.
Latest release June, 2015 - GameDev annoucement

Quote: Original post by Roots
C is better than C++ when it comes to performance; I don't think many people will argue with that.

Nonsense.
Quote: Original post by Roots
C is better than C++ when it comes to performance; I don't think many people will argue with that.


Oh yes they will. I'm one of them. Aside from restrict pointers in C99, which are offered by extension by many compilers for C++, there are almost no constructs in C that cannot be done in C++ with the exact same performance characteristics. However, there are constructs in C++ that can used without performance equivalent versions in C. For example, the C library qsort() function uses function pointers for user defined comparisons, which means that the comparison function cannot be inlined. The C++ library std::sort() function instead uses template function object arguments which can be inlined.
Quote: Original post by Roots
C is better than C++ when it comes to performance

Bullshit.

99.99% of the "fast" things you can do in C you can do in C++.

And lets compare idiomatic C and C++ with strings.

In C, strlen(char*) is an O(n) operation.
In C++, std::string::size() is an O(1) operation.

In C, to, say, append strings in an efficient manner, one has to manually keep track of the size of a char* array in a separate variable, which includes passing it to any relevant subfunctions. Most API functions that read char* don't accept corresponding size variables either.

In C++, to append strings in an efficient manner, one can simply use the built in appending functions. Since the size is tracked as part of the class, one doesn't need to manage, track, and pass a separate value, and any time you pass the string to a sub function it automatically gets that information passed as a result. In the end, you get the same performance for 20% of the code and 10% of the work, and all that time saved can be spent optimizing your bottlenecks.

So how is C faster here? For such a basic operation as string manipulation?

This topic is closed to new replies.

Advertisement