C++ array causes crash if member of class -
i'm working particular library not built stl containers. in refactoring functions classes encountered stack overflow based on following pattern.
class base { float values[1920 * 1080]; // causes overflow public: base() {} }; int main() { float values[1920 * 1080]; // not base t; } i know may allocate dynamic memory base::values, why not cause stack overflow in main, in base, why stack space seem smaller base? perhaps it's obvious i'm missing.
(above example compiled using visual studio 2017, default flags)
1920 * 1080 * sizeof(float) sufficent blow stack. (8 mb)
ensure compiler not remove values array setting elements.
change base follows.
class base { float * values; base() { values = new float[1920*1080]; } ~base(){ delete [] values; } } also fix copy , assignment operators.
Comments
Post a Comment