Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

There's nothing wrong with new[] an array of byte-objects containing constructors, right?

Tags:

c++

icc

In my physically based renderer, I'm getting a memory corruption bug (the program crashes, and the debugger gives a bogus stack trace that's worthless). I traced it down to this SSCCE. The line with the constructor seems to be what triggers the error:

#include <cstdint>

class Foo final {
    public:
        uint8_t packed;

    public:
        inline Foo(void) : packed(0xFF) {} //causes error
        inline ~Foo(void) = default;
};
static_assert(sizeof(Foo)==sizeof(uint8_t),"Implementation error!");

int main(int /*argc*/, char* /*argv*/[]) {
    Foo* arr = new Foo[4]; //Tried a bunch of different sizes.  All fail.
    delete [] arr;

    return 0;
}

The problem does not occur for MSVC or GCC, only Intel Compiler (the version of which is 16.0). But, since this is a memory corruption bug, that doesn't really mean anything. Before I submit a bug report, can someone confirm this is not me misusing C++?


Here is a premade solution demonstrating the problem. Here is the assembly.

like image 819
imallett Avatar asked Mar 17 '16 15:03

imallett


1 Answers

As established in the comments, over a series of increasingly simpler examples (and corresponding edits), this is perfectly valid C++ code.

I posted a bug report on Intel's developer forum, and it has been officially confirmed as such.

like image 198
imallett Avatar answered Nov 10 '22 16:11

imallett