mercredi 13 octobre 2021

c++11 program crashed with four dimension std::array [closed]

I just wrote a simple program like this:

constexpr unsigned RxLen = 10000; // in mm
constexpr unsigned RyLen = 10000; // in mm
constexpr unsigned RzLen = 4000; // in mm

constexpr unsigned scale = 1000; // in mm
constexpr unsigned NumX = (RxLen / scale);
constexpr unsigned NumY = (RyLen / scale);
constexpr unsigned NumZ = (RzLen / scale);

using cell = array<unsigned, AncGroupNeeded>;
//using cell = unsigned;
using Xcell = array<cell, NumX>;
using Yxcell = array<Xcell, NumY>;
using Zyxcell = array<Yxcell, NumZ>;


Zyxcell samples;
//int toto[2520];

int samplesTask() {
    unsigned xi = 0;
    unsigned yi = 0;
    unsigned zi = 0;

    for (xi = 0; xi < NumX; ++xi) {
        for (yi = 0; yi < NumY; ++yi) {
            for (zi = 0; zi < NumZ; ++zi) {
                std::cout << xi << ", " << yi << ", " << zi << std::endl;
                samples[xi][yi][zi][0] = 0;
            }
        }
    }

    return 0;
}

int main() {
    samplesTask();
    return 0;
}

it crashed with segment fault. However if I uncomment the line:

int toto[2520]

it runs without crash.

I think both array (samples, toto) are in the .bss section, the size of samples should be 10 * 10 * 4 * 3 *4 bytes = 4800 bytes, which is not very large.

I don't understand why toto[2520] can make it work, any ideas ?

Aucun commentaire:

Enregistrer un commentaire