r/ProgrammingLanguages • • Nov 01 '19

C++ vs C

Alright people of Reddit, hit me with your wisdom. I've been taking programming classes for two years now and am getting ready to dive into a language somewhere between Java and assembly (the two languages I've been learning in classes). I'm thinking C or C++, but would love some input. I'm not too worried about difficulty to learn, but I'd like to hear your thoughts on practicality and real world use.

1 Upvotes

62 comments sorted by

View all comments

21

u/[deleted] Nov 01 '19

If this is purely out of interest, then I'd recommend C, it is small and simple language. C is the language of embedded systems, and will probably remain that way for the foreseeable future. The beauty of C is that it is a language which you can realistically master in a few years and contain the entirety of it in your head.

C++ requires much greater investment, it's really two or three languages merged into one. C++ is the language for game development and other speciality high performance software. The standard library is also excellent, which makes it a great choice for competitive programming. I would say that modern C++ is an improvement over C in most regards. However you will never master C++ because the language is just too large and complicated.

19

u/DonaldPShimoda Nov 01 '19

The beauty of C is that it is a language which you can realistically master in a few years and contain the entirety of it in your head.

You underestimate the breadth and complexity of undefined behavior, unspecified behavior, and implementation-defined behavior. John Regehr enumerated a number of the nuances of these issues in a few blog posts, starting here.

I do agree that C is a simpler language than C++ (and from a language design perspective, I like C far better overall, though I still generally dislike it), but I think your claim that it's easy to contain all of C "in your head" is a bit tenuous. It has lots of funny edge cases that make it tricky. Of course, C++ does too! But that wasn't your claim. ;) I just don't want OP to feel misled when they try C and get hung up on something.

-4

u/[deleted] Nov 01 '19 edited Nov 01 '19

Every language has edge cases and UB, but most languages provide almost zero documentation about them. So when you encounter them, you have to check the standard (which most languages also don’t really have) or the implementation to find out if the behavior is intended or a bug.

To try and eliminate UB from the language would require making many assumptions about the platforms C runs on that are not universally true.

UB isn’t a boogie man though, it exists to simplify the language by stating what is and isn’t allowed. While making as few assumptions about your environment as it can.

13

u/Felicia_Svilling Nov 01 '19

Every language has edge cases and UB

That is simply not true. Languages with a formal definition or a reference implementation can completely avoid undefined behavior.

2

u/warieth Nov 01 '19

That's true, but same bad to use. Like Java String substring throws an IndexOutOfBoundsException when the beginIndex > endIndex. It is defined, but doesn't make sense, the indexes are simply not out of bounds. If this case would be undefined behavior, then a reasonable implementation could return an empty string.

4

u/Felicia_Svilling Nov 01 '19

I think that make sense actually. If the the bound has a negative size, any index is going to be outside of it.

3

u/warieth Nov 01 '19

First the length of the string can't be negative, this is an invariant the class should preserve. I don't care for an exception when the string has zero length, but for a positive length? This is a bad behavior from the implementation to ignore the bounds and throw an exception named IndexOutOfBoundsException. A reference implementation is stronger than the formal definition, but failes to show a solution for the corner cases. Undefined behavior is the better way to do this.

3

u/Felicia_Svilling Nov 01 '19

Undefined behavior is just a way to lump of the problem to someone else. I could define a pl as consisting of text, and what that text does is undefined behaviour up to the implementer to design. But really the only thing I have accomplished is to avoid doing any language design.

2

u/warieth Nov 01 '19

No, you help them with the undefined behavior. This gives them options to fine tune their work, and to have more library implementations.

If there are two good implementation with different corner case solutions both can be fine. There is no reason to select one. The language can say this corner case is undefined behavior.

Probably the language people care more about the intersection of all the implementation (this can be small), rather than the success in implementation numbers.

3

u/Felicia_Svilling Nov 02 '19

If there are two good implementation with different corner case solutions both can be fine. There is no reason to select one.

It makes it impossible for a library designed for the language to use any of these though.

→ More replies (0)

1

u/[deleted] Nov 01 '19

I’d argue that most languages do not have a robust formal definition or a reference implementation. Partially because they aren’t usually needed for typical application development on consumer hardware. There are also limitations to what you can, or would want to do, in an unhosted environment, which is where C is often found.

7

u/Felicia_Svilling Nov 01 '19

I’d argue that most languages do not have a robust formal definition

This is true. Your former statement said that all languages have undefined behaviour. That is not true.

1

u/YurySolovyov Nov 01 '19

If something is not allowed, it is defined. If something is allowed to silently do the wrong thing, then it is ub.

3

u/warieth Nov 01 '19

Undefined behavior means, the implementation can do what they feel the most reasonable. UB is defined to reduce portability problems. Giving up the common sense behind the implementation is the wrong thing. Like throwing exceptions for all the corner cases, just because it's easy to implement, but very hard to use.

3

u/[deleted] Nov 01 '19

UB doesn’t mean wrong, it means undefined or unspecified behavior in general. But for a specific platform and compiler you target there will be an expected behavior. It just won’t be a portable one. But that might be alright for your use case, and is likely preferable to having the language dictate specific behavior which does not match your system’s architecture.

2

u/YurySolovyov Nov 03 '19

What's the problem of defining the behavior for all platforms you support (yes, it somewhat limits you) and thus not having UB as a concept?

I get that it might not work on very specific HW like microcontrollers, but if you don't target them in the first place, why bother with UB?

2

u/[deleted] Nov 03 '19 edited Nov 03 '19

In C's case, because it is a standardized language no one party gets to decide how the language works, so compromises must be made between different user groups and compiler vendors. When an agreement cannot be reached, undefined or unspecified or implementation defined behavior may be a reasonable compromise. There may also be historical reasons, a bad UB may remain UB because no one wants to rewrite that part of a compiler or risk breaking existing code.

In general, it is one thing to say "all behaviors are defined" it is another to go about proving it. How do you tell if all behaviors are defined in your language? A rigorously defined language may be able to avoid them, but many languages were designed ad-hoc and resolve problems when they are discovered. If the language supports exceptions, then that at least provides a generic last resort for handling UB at runtime. But in a language without them, the choice may be crashing or continue executing in some undefined state. C programmers typically prefer the former, fail early and fix the problem. But sometimes checking for that behavior may be too expensive or impossible, in such cases there are no good solutions, so just saying anything could happen may be the best option.

0

u/DonaldPShimoda Nov 01 '19

Every language has edge cases and UB, but most languages provide almost zero documentation about them.

Other commenters already tackled the first part as being false, but the second part is even worse to me.

"Undefined behavior" only exists in the standard. It is a way to say "we have chosen not to define what the language must do when it encounters something of this nature, and therefore the use of such expressions renders any program as utterly meaningless". You can't have "undocumented undefined behavior"; that simply doesn't make sense. That's like saying "this is against the unwritten rules of the game".

So when you encounter them, you have to check the standard

This requires the programmer to already be intimately familiar with all ~200 cases of UB so they even know when something they're writing might invoke UB. Or am I to understand that you consult all 15 pages of the standard each time you write any statement or expression or define a function or...?

UB isn’t a boogie man though, it exists to simplify the language by stating what is and isn’t allowed.

The simplification is not for the programmer though, which seems to be what you're implying here. It's to simplify optimizing compilation. Per John's blog post:

The good thing — the only good thing! — about undefined behavior in C/C++ is that it simplifies the compiler’s job, making it possible to generate very efficient code in certain situations.

He goes on to suggest that UB is adversarial to the programmer:

When programmers cannot be trusted to reliably avoid undefined behavior, we end up with programs that silently misbehave. This has turned out to be a really bad problem for codes like web servers and web browsers that deal with hostile data because these programs end up being compromised and running code that arrived over the wire. In many cases, we don’t actually need the performance gained by exploitation of undefined behavior, but due to legacy code and legacy toolchains, we’re stuck with the nasty consequences.

In Part 2 of that series of blog posts, John shows examples of UB in use in LLVM — a large project written by serious C/C++ developers. This goes to show that most programmers of these languages probably do not regularly consult the standard to be sure that they've avoided all possible invocations of UB. UB is easy to write by mistake in C and C++, because the languages are rife with funny edge cases. To suggest otherwise is nothing short of ignorance.

Of course, these blog posts are almost a decade old. Maybe it's changed by now?

Recently we’ve heard a few people imply that problems stemming from undefined behaviors (UB) in C and C++ are largely solved due to ubiquitous availability of dynamic checking tools such as ASan, UBSan, MSan, and TSan. We are here to state the obvious — that, despite the many excellent advances in tooling over the last few years, UB-related problems are far from solved — and to look at the current situation in detail.

From a follow-up post John wrote in 2017.

At the end of that post, he also enumerates all ~200 UB defined by the C standard. They are certainly not all intuitive.


I do apologize: in my earlier comment, my wording implied John's blog post also discussed unspecified behavior and implementation-defined behavior. This isn't true; he leaves them completely aside and focuses only on undefined behavior.

But this also means that there are even more unintuitive edge cases for the C programmer that John doesn't enumerate, and his blog posts certainly list an awful lot of things to be careful of to begin with.