News for hackers. Stuff that matters.
Re:Zeroing Memory is Hard: VC++ 2015 arrays by AndrewGaspar on Monday July 18, 2016 @01:36AM (#12113385)
If you need to manually define a default constructor, and you want the behavior of default-initializing all members, declare the constructor as:

    T() = default;
Re:Zeroing Memory is Hard: VC++ 2015 arrays by byuu on Monday July 18, 2016 @12:45PM (#12116233)
If you want to feel some real pain ... take the following code (simplified for posting in this thread):

    template<typename T> struct Function {
      Function() = default;
      void* callback = nullptr;
    };

    struct CPU {
      Function<void ()> functions[65536];  //= {0} makes no difference
    };

    int main() {
      CPU cpu;
      return 0;
    }
There's a bug in GCC, at least versions 5.1 - 5.4, where the above code will take 30s-90s to compile (depends on your CPU speed), and eat 500MB+ of RAM in doing it. But the bug isn't in 4.9, and possibly newer GCC releases.

What causes it? "Function() = default;" of course. Change it to "Function() {}" and the code compiles in 100ms.

So because I can't tell people, "don't use GCC 5.x", or "don't ever use any of my class objects in a large array", I basically can't use "= default;" syntax on my constructors in any of my library code now :(

Re:Zeroing Memory is Hard: VC++ 2015 arrays by typelist on Monday July 18, 2016 @02:18AM (#12113482)
Explicitly defaulting the constructor behaves the same as an empty constructor: all members are default-initialized. The problem is usually that 'default-initializing' doesn't zero out primitive (POD) types, but leaves them uninitialized.
Re:Zeroing Memory is Hard: VC++ 2015 arrays by ryuuchin on Monday July 18, 2016 @08:36PM (#12118809)
With msvc the /sdl flag can zero initialize class members to zero[1] (among other things). This runs before the constructor. Aside from the warnings that get turned into errors (you can disable the warning to bypass this behavior) it also does limited pointer sanitization and turns on strict_gs_check.

[1] https://msdn.microsoft.com/en-us/library/jj161081.aspx

Re:Zeroing Memory is Hard: VC++ 2015 arrays by CamperBob2 on Monday July 18, 2016 @03:53AM (#12113729)
The problem is usually that 'default-initializing' doesn't zero out primitive (POD) types, but leaves them uninitialized.

Exactly, that's what hosed me. Some double-precision floats ended up with garbage values and I couldn't see how it was happening.

Re:Zeroing Memory is Hard: VC++ 2015 arrays by gpderetta on Monday July 18, 2016 @07:03AM (#12114337)
Explicitly using the T(), T{}, T = {} syntax will value-initialize the object, which will zero-initialize trivially constructible types.
Re:Zeroing Memory is Hard: VC++ 2015 arrays by typelist on Monday July 18, 2016 @12:01PM (#12115864)
Ah, interesting. Apparently this is true even if some constructors are explicitly '= default' (for the T x{} and T x={} list/aggregate initialization cases only).

But I believe that rule doesn't apply for any objects where adding an '= default' constructor would be useful, because it requires that there be no user-defined constructors or private/protected members. Otherwise that syntax ultimately calls the default constructor, which won't do any zeroing.

See http://en.cppreference.com/w/cpp/language/aggregate_initiali...

Re:Zeroing Memory is Hard: VC++ 2015 arrays by AndrewGaspar on Monday July 18, 2016 @11:47AM (#12115757)
Yes, forgot to mention this crucial detail.