Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Segfault in Function call

Tags:

c++

clang++

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();
}
like image 899
user1937198 Avatar asked Jun 25 '26 10:06

user1937198


1 Answers

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.

like image 115
Sneftel Avatar answered Jun 27 '26 01:06

Sneftel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!