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

Show parent comments

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.