c++ - Static array overflows stack (seg fault 11) only when inside class definition, but not otherwise...? -
i'd allocate large static array (-- want avoid dynamic allocation, e.g. std::vector, or using 'new', can guarantee physical addresses consecutive , can prefetched effectively). array size determined external factors, they're known ahead of time -- in example, i'll need 3211264 elements.
if allocate float array, works fine:
#include <iostream> #include <cstdlib> using namespace std; float f[3211264]; int main() { for(int = 0; < 3211264; i++) f[i] = rand(); for(int = 0; < 3211264; i++) cout << f[i]; }
however, if wrap array within class, causes seg fault:
#include <iostream> #include <cstdlib> using namespace std; class t { public: t(); private: float f[3211264]; }; t::t() { for(int = 0; < 3211264; i++) f[i] = rand(); for(int = 0; < 3211264; i++) cout << f[i]; } int main() { t myt; }
how memory being allocated, , there reason why it's different when wrap data inside class?
i'd hope class overhead minimal, , object-based re-alignment or padding at-most page side (4k), right? me, class readability , organization, if it's going have major impact on performance, i'd ditch , declare bunch of globals...
my compiler info:
configured with: --prefix=/applications/xcode.app/contents/developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
apple llvm version 7.3.0 (clang-703.0.29)
target: x86_64-apple-darwin15.6.0
thread model: posix
in code make array class member not being statically allocated. if said:
static t myt;
though can't believe allocating statically give serious performance increase.
Comments
Post a Comment