News for hackers. Stuff that matters.
Re:A bug in GCC that may cause memory leaks in valid C++ pro... by vvanders on Thursday April 27, 2017 @04:18PM (#14214338)
Good stuff.

Only compiler bugs I've seen in C++ have all been around exceptions. I think this is just another reason towards force-handled error codes. You don't have a whole new control flow that needs to be verified and is much simpler from an implementation/runtime perspective.

Re:A bug in GCC that may cause memory leaks in valid C++ pro... by codedokode on Friday April 28, 2017 @02:14AM (#14217206)
I disagree. You are suggesting everyone should write error handling code manually rather than rely on compiler.
Re:A bug in GCC that may cause memory leaks in valid C++ pro... by vertex-four on Friday April 28, 2017 @05:10AM (#14217958)
Not really. See Rust/Scala/Haskell's Result/Either type, which has compiler support, but that compiler support is lightweight - you use the same control structures and flows as with regular code.
Re:A bug in GCC that may cause memory leaks in valid C++ pro... by lmm on Friday April 28, 2017 @07:50AM (#14218643)
That's a great approach, but needs sum types and HKT to be effective, neither of which C++ really has (std::variant exists but doesn't always behave as a proper sum type AIUI) at present.
Re:A bug in GCC that may cause memory leaks in valid C++ pro... by vertex-four on Friday April 28, 2017 @09:17AM (#14219093)
Rust manages it without HKT quite reasonably (rather than implementing generic monads, it has the `?` operator, which is syntax-sugar for something like [0]), though sum types are a necessity.

[0] match whatever { Ok(v) => v, Err(e) => return Err(From::from(e)) }

Re:A bug in GCC that may cause memory leaks in valid C++ pro... by lmm on Friday April 28, 2017 @09:38AM (#14219233)
Ah, last I knew they were relying on macros. Still, ? is an ugly special case where with HKT it could be a reusable general operator.
Re:A bug in GCC that may cause memory leaks in valid C++ pro... by vertex-four on Friday April 28, 2017 @10:00AM (#14219405)
Absolutely - there's currently discussion around a Carrier trait which would make it generic, once we have some variety of HKT. Still, it's good enough for now, and any future development of it will be backwards-compatible.
Re:A bug in GCC that may cause memory leaks in valid C++ pro... by wvenable on Thursday April 27, 2017 @04:34PM (#14214463)
It doesn't solve the problem because you'd have move to 2 stage construction/initialization and that creates even more problems than this.
Re:A bug in GCC that may cause memory leaks in valid C++ pro... by vvanders on Thursday April 27, 2017 @04:51PM (#14214583)
Nope, if you have an operation that can fail then wrap it in a type that represents that(I'm partial to Rust's Result<T,E>) then you force consumers to deal with potential errors and you can do 1-call construct + init if you want to.

Exceptions cause all sorts of problems(like violating the principal of only paying for what you use). They aren't checked so you can't enforce handling via type system and in general they're just a mess in C++.

Re:A bug in GCC that may cause memory leaks in valid C++ pro... by int_19h on Thursday April 27, 2017 @07:38PM (#14215770)
> Exceptions cause all sorts of problems(like violating the principal of only paying for what you use).

Exceptions are zero-cost in terms of performance on any sane architecture (this excludes x86, but not x64). The only cost that you're paying is the extra memory for the tables, which are all statically allocated.

Re:A bug in GCC that may cause memory leaks in valid C++ pro... by muizelaar on Thursday April 27, 2017 @10:44PM (#14216544)
Compiling with exceptions can still have performance cost because the addition of implicit control flow at every function call.

For example the following code will compile differently depending on whether goo() can throw.

struct k { void p[4]; };

void goo(k s);

k foo() { k r; k s; goo(&s); r = s; return r; }

If goo() doesn't throw the extra variable s can be eliminated.

Re:A bug in GCC that may cause memory leaks in valid C++ pro... by int_19h on Friday April 28, 2017 @01:03AM (#14216971)
If goo doesn't throw, why not declare it nothrow?

Although I still don't see why, in your example, one of the variables cannot be elided even if goo does throw. Since there's no point at which address of r is taken, much less escapes the scope, it should always be safe to elide r and just return s, no?

Re:A bug in GCC that may cause memory leaks in valid C++ pro... by aristidb on Thursday April 27, 2017 @10:54PM (#14216574)
Try indenting your code section by 4 spaces. :)
Re:A bug in GCC that may cause memory leaks in valid C++ pro... by flamedoge on Friday April 28, 2017 @02:18AM (#14217222)
you mean r? you can't eliminate s regardless of goo throwing.
Re:A bug in GCC that may cause memory leaks in valid C++ pro... by dbaupp on Thursday April 27, 2017 @09:18PM (#14216219)
They're zero(ish) cost at runtime, but focusing on just that is missing something: pervasive unchecked exceptions inhibit optimisations, both in compilers and in libraries, which have to be more defensive to preserve correctness.
Re:A bug in GCC that may cause memory leaks in valid C++ pro... by int_19h on Friday April 28, 2017 @12:56AM (#14216946)
If you replace exceptions with e.g. return values, it doesn't really change anything for the libraries - if they expect to be able to deal with an error, they still have to defensively preserve correctness.

OTOH the library always has the option to say "this particular thing that you give me should be nothrow; if it throws, that's U.B.", and not even try being defensive in the face of exceptions when they come out of such a thing.

Re:A bug in GCC that may cause memory leaks in valid C++ pro... by dbaupp on Friday April 28, 2017 @04:02AM (#14217690)
I think you missed the precise qualification of my comment: this only applies when the compiler can't tell what exceptions can occur, like in C++, Rust and even Java, and so needs to be able to handle any/all of them. The most basic (and most relevant) query is "can this call throw?", but even that is impossible to answer without global information when there are unchecked exceptions.

In statically typed languages, return values, like (fully) checked exceptions, are different, as they're exactly the sort of global information the compiler needs. Functions can statically guarantee the exact set of ways in which it can interact with its caller, and the compiler can understand. This means a function that, say, sorts a list with a less-than function (that returns bool) knows that every time that function exits, it gives a value of true or false, no exceptions (hah).

In any case, just writing a library that says "UB to throw" isn't quite enough, as the compiler won't understand, and will still structure code to handle exceptions "sensibly", even thought it doesn't need to. Fixing that means being able to communicate nothrow to the compiler (which is nice too), but this starts to no longer be a pervasive unchecked error system: it's somewhat of a binary form of checking.

Re:A bug in GCC that may cause memory leaks in valid C++ pro... by int_19h on Friday April 28, 2017 @04:34PM (#14222839)
Well, we were talking about exceptions in C++, where there is a standard way to communicate nothrow. So yes, you're right that it's no longer a pervasively unchecked error system - but that's precisely why they're not any worse than Result<T,E> at this point. Effectively you can treat C++ as having every function return Result<T,E> by default, with nothrow being an opt-out to just return T.
Re:A bug in GCC that may cause memory leaks in valid C++ pro... by milesrout on Friday April 28, 2017 @02:00AM (#14217147)
The thing is, in C++ you have to explicitly write noexcept for that to happen.

If you use optional<T> you have to explicitly write that, and turning off exceptions means you don't have to write noexcept everywhere.

Re:A bug in GCC that may cause memory leaks in valid C++ pro... by searealist on Friday April 28, 2017 @12:05AM (#14216802)
I guess that's why you Rust guys don't have exceptions, so you don't have to pay this cost. </s>
Re:A bug in GCC that may cause memory leaks in valid C++ pro... by dbaupp on Friday April 28, 2017 @12:09AM (#14216820)
(Explanation of the /s: panics use the same mechanism, so the compiler and libraries unfortunately still have to be just as defensive when they're enabled. When the compiler is in -C panic=abort mode, it doesn't suffer from this, but I don't think libraries have a great way to customise/improve their behaviour in that case at the moment.)
Re:A bug in GCC that may cause memory leaks in valid C++ pro... by vvanders on Thursday April 27, 2017 @07:52PM (#14215835)
Don't you need RTTI as well? That's not been zero-cost on all compilers I've used in the past.
Re:A bug in GCC that may cause memory leaks in valid C++ pro... by int_19h on Friday April 28, 2017 @12:57AM (#14216953)
You do, but C++ includes RTTI separately from exceptions (dynamic_cast and typeid), so it's going to be there regardless. If you turn it off, then we're not talking about standard C++ anymore, but some subset.
Re:A bug in GCC that may cause memory leaks in valid C++ pro... by wvenable on Friday April 28, 2017 @01:33AM (#14217062)
When you throw and exception, C++ compilers add RTTI for those classes even when RTTI is separately disabled. You cannot call dynamic_cast and type_id but the generated code is the same.

I don't really see this as much of an issue given you have to have this RTTI to catch the exception properly.

Re:A bug in GCC that may cause memory leaks in valid C++ pro... by int_19h on Friday April 28, 2017 @03:42AM (#14217604)
My point is that RTTI is required by other things in the C++ standard, not just exceptions. So it's still "free" if we're only considering exceptions separately by themselves, and leave all other features as they are in standard C++ (and if you disable RTTI entirely, then you no longer have standard C++, since those other features also stop working).

If we consider the combo of exceptions+RTTI, then yes, dropping both at the same time lets you get rid of some further overhead (although again it's memory-only, not compute cycles). But then we have to discuss the lost benefits of those other features, not just exceptions alone, to judge whether doing so is worthwhile or not.

BTW, I may well be wrong here, but isn't RTTI generation for exceptions (when it's otherwise disabled) an example of "only pay for what you use"? i.e. wouldn't the compiler then only generate RTTI entries for those types that are either thrown somewhere, or are caught somewhere? If so, any library code that doesn't throw itself, and only needs to allow exceptions to pass through it, is still zero-overhead in that regard.

Re:A bug in GCC that may cause memory leaks in valid C++ pro... by easytiger on Friday April 28, 2017 @04:30AM (#14217825)
> Exceptions are zero-cost in terms of performance on any sane architecture

Not when the exception is triggered

Re:A bug in GCC that may cause memory leaks in valid C++ pro... by int_19h on Friday April 28, 2017 @04:35PM (#14222847)
If you trigger an exception, then you're not paying for something that you don't use anymore.
Re:A bug in GCC that may cause memory leaks in valid C++ pro... by Arnavion on Thursday April 27, 2017 @05:23PM (#14214829)
C++ constructors can't return error codes or Result. That's the two-stage initialization (constructor call + initialize() call) wvenable is talking about.

(Of course you could do the same thing as Rust and expose only a static creator function and hide the actual constructor.)

Re:A bug in GCC that may cause memory leaks in valid C++ pro... by vvanders on Thursday April 27, 2017 @05:34PM (#14214916)
Yeah, most projects I worked on was doing the static creator before Rust showed up on the scene since it sidesteps a lot of these issues.
Re:A bug in GCC that may cause memory leaks in valid C++ pro... by petters on Friday April 28, 2017 @02:01AM (#14217151)
Exceptions are typically faster than error codes when errors are rare.
Re:A bug in GCC that may cause memory leaks in valid C++ pro... by saghm on Thursday April 27, 2017 @07:51PM (#14215827)
In terms of memory usage for similar Result type in C++, is there any way around allocating the space for both types (as opposed to Result in Rust, which just allocates enough space for the larger of the two)? My understanding is that there is not, although I'm still not very experienced with C++, so I wouldn't be surprised if I'm wrong
Re:A bug in GCC that may cause memory leaks in valid C++ pro... by vvanders on Thursday April 27, 2017 @07:57PM (#14215866)
You can use a union + tag(which the same as Rust uses which is why they're called Tagged Unions) however you run into issues with constructors/destructors. You can do something similar with placement new/delete but it requires some hoops.

Honestly most error codes are small enough that you can just pay the hit for it on the stack and discard it once you've handled the good/bad case.

Re:A bug in GCC that may cause memory leaks in valid C++ pro... by gpm on Thursday April 27, 2017 @08:18PM (#14215947)
std::variant with c++17, but on linux at least you need to install gcc from git to have access to it so far.

boost::variant otherwise, but depending on the types used (if there aren't any that are nothrow default constructible) it allocates on assignment.

If you just want a result type and not a more general "rust enum", this repository does it (while only allocating space for the larger, I think) https://github.com/oktal/result/

And I don't think there's any reason you couldn't implement an equivalent of std::variant for previous versions of C++, I'm just not aware of any.

Re:A bug in GCC that may cause memory leaks in valid C++ pro... by irishcoffee on Friday April 28, 2017 @09:38AM (#14219226)
Heh, I implemented an in-house variant at work a week ago. As it turns out, I was able to use my variant class in a python extension I developed. Also, as it turns out, python is scary in what it lets you get away with when developing extensions.
Re:A bug in GCC that may cause memory leaks in valid C++ pro... by Ace17 on Friday April 28, 2017 @12:43AM (#14216907)
Just because a language feature is hard to implement doesn't mean we should abandon it.

I'm not going back to explicit error handling.

Re:A bug in GCC that may cause memory leaks in valid C++ pro... by warmwaffles on Friday April 28, 2017 @12:56AM (#14216947)
Come on in, the water is fine. Handling errors without using exceptions is not that bad.
Re:A bug in GCC that may cause memory leaks in valid C++ pro... by geezerjay on Friday April 28, 2017 @02:34AM (#14217288)
It's still very bad, though. Exceptions control that badness. There's no excuse to avoid using a useful feature for reasons that actually worsen your code.
Re:A bug in GCC that may cause memory leaks in valid C++ pro... by easytiger on Friday April 28, 2017 @04:29AM (#14217815)
Exception handling is computationally very very expensive when triggered. a cmp op costs nearly nothng.
Re:A bug in GCC that may cause memory leaks in valid C++ pro... by saurik on Friday April 28, 2017 @04:37AM (#14217848)
Exception handling costs absolutely nothing when not triggered (on most reasonably platforms these days, due to zero-cost exceptions), while that comparison instruction is still there, still taking its performance cost for every call of every function that could theoretically fail in the entire program.
Re:A bug in GCC that may cause memory leaks in valid C++ pro... by pdpi on Friday April 28, 2017 @08:14AM (#14218750)
If it's a hot path, branch prediction will almost certainly make the cost of that comparison negligible though. If it's not a hot path, does it matter?
Re:A bug in GCC that may cause memory leaks in valid C++ pro... by sqeaky on Friday April 28, 2017 @09:52AM (#14219334)
The difference in cost is negligible compared to the difference in correctness. Vtables are so optimized (and often elided) and caches so efficient and comparisons are cheap and branch predictors so great that the cost of both of these is functionally 0 in all code I touch outside the tightest inner in physics sims and rendering pipelines. Even there the differences is statistically negligible.

On correctness, it is impossible to forget to deal with an exception, it is Tuesday when a C program forgets to check an error code. Did you know printf returns an error code?

There are ways to force these returns to be checked by the compiler, but a language needs to be structured around it to be sane otherwise you wind up with every printf in an if statement. Forced exception checking is almost as burdensome, anybody with a few years of java ought to agree or at least see where I am coming from.

Error handling is just plain hard. Only a few languages do it really well. I think either could be done right if baked in from the first language spec. But that is not where most code is written, most Java, C and C++ has to exist now. Clearly C has only error codes, and it has the most bugs. Java has exceptions and finally to force the dev to handle everything right there, which I think is a huge source of anti-patterns but better than error codes. C++ has exceptions and destructors which work the best of these, but still separate error handling from code execute all the time when it is only desirable most of the time.

Re:A bug in GCC that may cause memory leaks in valid C++ pro... by jhasse on Thursday April 27, 2017 @04:22PM (#14214372)
I think this would just move the bugs to the applications.
Re:A bug in GCC that may cause memory leaks in valid C++ pro... by vvanders on Thursday April 27, 2017 @04:24PM (#14214387)
Hence the "force-handled" part of my comment :).
Re:A bug in GCC that may cause memory leaks in valid C++ pro... by gizmo686 on Thursday April 27, 2017 @05:55PM (#14215088)
But what you need is force-handled-correctly. If you think that programmers will correctly deconstruct partially constructed objects at every location where construction might fail, then you have far too much faith in our profession.
Re:A bug in GCC that may cause memory leaks in valid C++ pro... by josefx on Friday April 28, 2017 @03:23AM (#14217515)
By that logic we should write hand optimized assembly. I think there was even a rant by Linus on GCC messing up the stack and he writes C. Clearly compilers shouldn't be trusted with important programs.
Re:A bug in GCC that may cause memory leaks in valid C++ pro... by user5994461 on Thursday April 27, 2017 @06:24PM (#14215302)
That makes sense. Exceptions in C++ are a mess. It's completely platform, architecture, OS and compiler version specific. That's not well defined.
Re:A bug in GCC that may cause memory leaks in valid C++ pro... by int_19h on Thursday April 27, 2017 @07:39PM (#14215773)
The behavior of exceptions in C++ is well-defined. But implementations do have bugs about many things that are well-defined - this is one of those cases.
Re:A bug in GCC that may cause memory leaks in valid C++ pro... by metaobject on Thursday April 27, 2017 @06:33PM (#14215375)
Where in the C++ spec are implementations of features defined?
Re:A bug in GCC that may cause memory leaks in valid C++ pro... by pjmlp on Friday April 28, 2017 @02:38AM (#14217303)
Of course it is not defined, that is a language specification not a compiler implementation guide.
Re:A bug in GCC that may cause memory leaks in valid C++ pro... by hermitdev on Thursday April 27, 2017 @05:23PM (#14214831)
A number of years ago, I hit against a GCC bug that generated an ICE (internal compiler error). Cause? Usage of an anonymous namespace in the global namespace. It was a short-lived regression, I think in the 4.1.2 series that came to me as a result of a supposedly minor OS update.