小编Muh*_*eel的帖子

运行以下代码时,为什么不出错?

我正在运行以下代码,其中我声明了一个动态2D数组,然后继续在列索引处分配值高于实际为动态数组分配的数字列.但是,当我这样做时,代码运行完美,我没有得到错误,我相信我应该得到.

 void main(){


        unsigned char **bitarray = NULL;
        bitarray = new unsigned char*[96];

        for (int j = 0; j < 96; j++)
        {
                bitarray[j] = new unsigned char[56];
            if (bitarray[j] == NULL)
            {
                cout << "Memory could not be allocated for 2D Array.";
                return;// return if memory not allocated
            }
        }

        bitarray[0][64] = '1';
        bitarray[10][64] = '1';

        cout << bitarray[0][64] << " " << bitarray[10][64];

        getch();
        return;
    }
Run Code Online (Sandbox Code Playgroud)

我得到的输出的链接在这里(值实际上是准确分配的,但不知道为什么).

c++ pointers undefined-behavior dynamic-memory-allocation visual-c++

-3
推荐指数
1
解决办法
105
查看次数