Question

Why does the following code run fine when compiled with gcc yet segfault when compiled with clang (3.4). If the apparently redundant Thread object is removed the code runs fine. Also if any of the arrays are made smaller the code runs fine.

#include <array>
#include <emmintrin.h>

class Thread {
public:
    std::array<__m128i,  16 * 3 * 3 * 1280> m_BlockTypes;
    unsigned int  m_SeedIdx1[16 * 16 * 3 * 3 * 512];
};

class BroadcastImpl
{
    public: 
    std::array<__m128i, 16 * 3  * 3 * 256> Evaluate()
    {
        return std::array<__m128i, 16 * 3  * 3 * 256>();
    }
};


int main(int argc, char** argv) {
    Thread thread;
    (void)(thread);
    BroadcastImpl().Evaluate();
    BroadcastImpl().Evaluate();
}
Was it helpful?

Solution

Well, you're allocating two huge arrays on the stack. The maximum stack size is not defined by the C++ standard (compilers choose their own limits there), but go over that maximum and stuff's gonna break.

Large arrays should be allocated on the heap, which means std::vector is a better choice than std::array (though you could dynamically allocate the std::array if you really wanted to). And you certainly shouldn't be returning an array of that size from a function without at least reading up on move-constructors.

OTHER TIPS

This seems to be a stack overflow. My guess is that clang's default stack size is different from g++'s. Try setting the stack size manually to the same size in both linkers.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top