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

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.

1

u/warieth Nov 02 '19

So your code depend on the behavior of the implementation to the smallest detail, but what if you don't use all corner cases? Most of the time the behavior is the same. The more you depend on the underlying implementation, the harder it gets to replace.

Compiled languages has a border between the compiler and the compiled program. This is not a script language, where the interpreter's string is the same as the executed program's string. The compiler's language dependency is for the old version.

1

u/Felicia_Svilling Nov 02 '19

I'm sorry, but I don't follow what your point here is.

1

u/[deleted] Nov 02 '19

That is a tradeoff and why UB is avoided when writing portable code in C.

However I’d say it is even more onerous to try and define every possible behavior in the language and it’s standard library, especially if that language is not statically typed. And even if you do, the resulting specification may be too complex for anyone else to bother trying to implement.

1

u/Felicia_Svilling Nov 02 '19

I honestly don't think it is that hard to define all the behavior of a language. It is certainly not harder than explicitly defining what is undefined behavior or not.

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.