2

私はこのような機能を持っています:

void QuadTree::alloc( Quad***& pQuadsArray ) {
    const int _quadsCount = 100;

    // allocates memory as one chunk of memory
    Quad** _data = new Quad*[_quadsCount * _quadsCount]; 
    pQuadsArray = new Quad**[_quadsCount];
    for( int i = 0; i < _quadsCount; ++i ) {
            pQuadsArray[i] = _data + i * _quadsCount;
    }
}

// calling like this:
Quad*** test = nullptr;
alloc( test );

それはうまくいきます。しかし、これはそうではなく、その理由はわかりません:

void QuadTree::alloc( Quad**** pQuadsArray ) {
    const int _quadsCount = 100;

    // allocates memory as one chunk of memory
    Quad** _data = new Quad*[_quadsCount * _quadsCount]; 
    *pQuadsArray = new Quad**[_quadsCount];
    for( int i = 0; i < _quadsCount; ++i ) {
            *pQuadsArray[i] = _data + i * _quadsCount; // code crashes here
            // tried *(pQuadsArray[i]) but it didn't help
    }
}

// calling like this:
Quad*** test = nullptr;
alloc( &test );

ここで何が問題なのですか?

4

1 に答える 1

5

演算子の優先順位に問題があります - 変更してください:

        *pQuadsArray[i] = _data + i * _quadsCount; // code crashes here

に:

        (*pQuadsArray)[i] = _data + i * _quadsCount;
于 2013-05-21T20:43:06.237 に答える