C++ Logo


Advanced search

Re: [ub] Undefined behaviour from uninitialised variables

From: Nevin Liber <nevin_at_[hidden]>
Date: Thu, 31 Oct 2013 12:29:41 -0500
On 31 October 2013 08:31, Peter Sommerlad <peter.sommerlad_at_[hidden]> wrote:

> Chris,
> I teach my C++11 students to always use curly braces when defining a
> variable or assign an auto variable from an expression. This way it is
> guaranteed to be initialized. Any code that doesn't use them is easy to
> spot.

In practice I've found this helps make the bugs repeatable at run time, but
rarely addresses the underlying problem. People ought to think about how
they want to initialize stuff.

And then there are annoying cases where that just doesn't work:

struct S
    S() : b{} () // does not do what you think it should do

    std::atomic<bool> b;

Like the pro-ub arguments, just initializing something makes it much harder
to analyze the code to find bugs.
 Nevin ":-)" Liber  <mailto:nevin_at_[hidden]>  (847) 691-1404

Received on 2013-10-31 18:30:37