C vs C++

Not about OpenMW? Just about Morrowind in general? Have some random babble? Kindly direct it here.
User avatar
raevol
Posts: 3093
Joined: 07 Aug 2011, 01:12
Location: Caldera

Re: C vs C++

Post by raevol »

I think any technical construction is going to have flaws once you dig deep enough.

Again, I am not the most experienced person, but here's the way I look at it:

Both C and C++ are compiled languages. So you're only going to use them when performance is an issue. You wouldn't write a web framework in a compiled language. Yes, I know someone is going to post an example of one to prove me wrong, but that's not the point. If your application is not performance intensive but is stability intensive and needs frequent code changes, don't use a compiled language.

I'm not aware of any other compiled languages out there that are in common use. There's assembly of course, but that only works if you are guaranteed to run your code on one set architecture. So we're stuck with these two for performance intensive applications. Until some other compiled language sees widespread adoption, these are what you've got.

And the important thing to understand about them, and what inspired me to start this thread, is that even though they are "similar", the approach that you take when programming in one vs the other is very different. If you are programming in C, try to think about what the assembly the compiler is going to generate is going to look like. Don't waste time dynamically allocating memory or branching to simple functions when you don't need to. Think about what your memory looks like, and write good clean code to handle your task quickly and simply. If you are programming in C++, you have a ton of language features at your disposal to make your code abstract and dynamic. This doesn't mean go crazy and OOP everything, but it does mean, in the right place and time, take advantage of the power that is available to you, and keep your code neat and concise with those features.
User avatar
Jyby
Posts: 408
Joined: 10 Dec 2013, 04:16

Re: C vs C++

Post by Jyby »

@raevol
Java is a compiled language
Image
User avatar
wareya
Posts: 338
Joined: 09 May 2015, 13:07

Re: C vs C++

Post by wareya »

@OP: There's basically no reason to write C instead of C-style C++ unless you're working on a project where only a C compiler is available, or if you don't have access to C++11 (even then, debatable).

C++11 and newer give C++ features that are basically vital to building modern nontrivial applications. Specifically, lambdas and type deduction. C++ (including pre-C++11) also sanitizes a lot of unsafe C OoP patterns, like datastructure instance factory functions/macros and removing the need to use struct packing just to get subtyping.

There IS a lot of bad C++ out there, including bad MODERN C++ like reference counted pointers (just use a deterministic GC...), but you can certainly do better with basic C++ than with pure C.

The only reason I would use java is because it's very IDE-friendly and has Maven (in fact, I did end up using java, for exactly those two reasons). The JVM is great, but I really don't like java as a language. It's like the boring parts of C++, though java 8 improves on that.
User avatar
raevol
Posts: 3093
Joined: 07 Aug 2011, 01:12
Location: Caldera

Re: C vs C++

Post by raevol »

Jyby wrote:Java is a compiled language
http://imgur.com/a/YZpSa.jpg
404. Also, don't you need the java runtime to run a java application?

Edit: just saw viewtopic.php?f=10&t=4306
User avatar
psi29a
Posts: 5359
Joined: 29 Sep 2011, 10:13
Location: Belgium
Gitlab profile: https://gitlab.com/psi29a/
Contact:

Re: C vs C++

Post by psi29a »

Jyby wrote:@raevol
Java is a compiled language
http://imgur.com/a/YZpSa.jpg
You sure about that? I could have sworn that you needed a virtual machine just to run the byte-code. I mean, Python does the exact same thing. I'm not sure if that brings down Python or picks up Java. ;)


https://en.wikipedia.org/wiki/Interpret ... a_bytecode
Java (is compiled into Java bytecode to be interpreted by JVM)
^-- woops
ezze
Posts: 513
Joined: 21 Nov 2013, 13:20

Re: C vs C++

Post by ezze »

The main problem is that nowadays the difference between compiled and interpreted is becoming a bit fuzzier.

The JVM (Java Virtual Machine) is for example allowed to recompile in machine native language functions that are often used, and I heard (but never checked myself) that it can recompile with function argument closures. The latter means that it can do _more_ than a classic compiled language.

On the other hand Clang C and C++ compiler is based on the Low Level Virtual Machine, but nobody would consider its results "interpreted".

About the other point, to measure the "speed of a programming language" there is no silver bullet. Even the question itself is badly defined in my opinion. I spoke with a google worker and he said that a common pattern over there is writing a quick and dirty solution in Python, that yes it's slow as execution time, but it is very quick for writing working programs and if it is enough to solve the problem, for example the routine is executed just once in a while, then the problem it's solved. If the new program is used often (possibly thousands of time per second!) then it needs a retouch and they often move to C++, but with already an idea how to solve the problem and something already working on the background.

Another important factor is the availability of libraries, if you want to make your own wiki website you can start from cppcms wiki demo. But when you see it stores the passwords in clear text you might consider that it is better using python-django or something.

Finally, what are your programmers expert in? As I mentioned in the beginning every language as its idioms and quirks, stating that "once learn the syntax you are done," it's a bit of a lie. This very project moved from D to C++ for lack of expertise in D (that I do not like personally).
wareya wrote:There IS a lot of bad C++ out there, including bad MODERN C++ like reference counted pointers (just use a deterministic GC...), but you can certainly do better with basic C++ than with pure C.
Very true, but as the sturgeon's law reminds us, it applies to everything. Still a problem with C++ is its power and expressiveness that brings "bad C++" to be overly complex without need. Simple example, if you add an operator bool to your class just because you think that calling .empty() is too wordy and not because there is some kind of true/false logical interpretation of your class you are making the life of future programmers bad without reason. In java it won't happen, because even if you are very good reason to, you cannot.
ezze
Posts: 513
Joined: 21 Nov 2013, 13:20

Re: C vs C++

Post by ezze »

sirherrbatka wrote:
Actually, I am fairly sure it comes from the C name spaces of identifiers so... yeah?
Nah, they could just have keyword new alongside, let's say, create. One allocates on heap, another on stack. But nobody thought about that.
Adding keywords is always a big problem. Stroustrup is always very keen in compatibility with the old code, that's the reason that override is a context-sensitive keyword. He does not want to break old code that uses it, you think he is wrong? I cannot say, try ask him.
sirherrbatka wrote:

Code: Select all

class foo {
 public:
  template<typename S>
  foo() {};
};
Oh yeah ... This is a classic. You cannot use it. There are workarounds, but I guess you don't care.
sirherrbatka wrote:
On the other hand there are not so many languages that go from low to high level and with such expression power.

That is poor man excuse. C++ provides you with all sorts of tools to introduce complexity, but not so many tools to manage it. For instance, where are concepts? Not in C++11, not in C++14, not sure about C++17… Syntax that can't do it's job does not make things any better. Besides, C++ is not so high level anyway.
I think you are greatly underestimating the difficulty of designing a programming language so largely used as C++, I strongly suggest you to start following the standard committee. Here.

Oh, well.
User avatar
Jyby
Posts: 408
Joined: 10 Dec 2013, 04:16

Re: C vs C++

Post by Jyby »

@psi29a and @ezze

You can turn off JIT compiling and create a binary directly. We do that for real-time / performance reasons at my company.

One big difference is the lack of interactive interpreters for Java. Java can't run arbitrary lines of code (I'm sure you can find an example where I'm wrong). But in JavaScript, an interpreted language, you can just pull up the inspector and just run your own code. Same with python.

D is just not really mainstream or ready. It's like Haskell it's just not, well good yet. Even John Carmack comes to the conclusion with regards to these new languages. What's interesting if you follow him on twitter is he even avoids some of the more esoteric features of C++ preferring the basics more for better maintaining and enforcing. So these advantages of C++ aren't as numerous on commercial projects, just from a project management perspective.
User avatar
psi29a
Posts: 5359
Joined: 29 Sep 2011, 10:13
Location: Belgium
Gitlab profile: https://gitlab.com/psi29a/
Contact:

Re: C vs C++

Post by psi29a »

Jyby wrote:You can turn off JIT compiling and create a binary directly. We do that for real-time / performance reasons at my company.
Yeah, it's real-time until the GC comes around and stops the whole thing... so yeah, not real-time. Not to be used in mission-critical systems.

I just find it really hard to recommend the use of Java these days except for when you have a legacy system you need to maintain/extend or your employees/peers are specialized in Java and it would be too difficult (or costly) to retrain them or let them go.

Can you recommend anything good about Java (as a language) that isn't covered by another modern language?
Chris
Posts: 1626
Joined: 04 Sep 2011, 08:33

Re: C vs C++

Post by Chris »

raevol wrote:stability intensive and needs frequent code changes, don't use a compiled language.
I would argue the opposite. One of the big advantages of compiled languages is static type safety. It's a lot harder to screw up types and using objects as the wrong type, if the compiler will error before it ever gets to a state where it can run. Interpreted and JIT languages make such issues a lot harder to spot since it'll only be seen when that code path is executed, and it'll be a lot harder to fix if it or other related code has been modified since the error was introduced.

If you don't care about run-time performance and want faster code turn-around, you can turn off optimizations to speed up compilation. And it'll save on unit tests that are solely designed to ensure objects are being used correctly.
wareya wrote:@OP: There's basically no reason to write C instead of C-style C++ unless you're working on a project where only a C compiler is available, or if you don't have access to C++11 (even then, debatable).
Depends. On certain systems, premade support libs that have the C++ runtime as a dependency can be a problem with deploying binaries. For instance, I once proposed changing OpenAL Soft to C++, in part because of Microsoft's continued horrid support for non-ancient C standards. The response I got back from other developers was an astounding "Please Don't", because if an app is distributed as a binary that uses its own packaged C++ runtime, and it links to OpenAL which pulls in the system's C++ runtime, Very Bad Things can happen.
Post Reply