无法初始化512x512阵列

use*_*196 0 c++ arrays

嘿所有我想只是为什么我在尝试初始化512x512阵列时不断出现堆栈溢出错误?有人可以帮忙吗?下面是我的代码的一部分

CImg<float> image("lena8bit.jpg"); 
CImgDisplay main_disp(image,"Main image");

    int ImgArray [512][512];
Run Code Online (Sandbox Code Playgroud)

基本上我想做的就是从图像中获取像素值并将其存储到此数组中.图像为512x512,因此是阵列大小.

希望听到你的回答,谢谢!

Mes*_*sop 5

您的数组太大,无法在堆栈上分配.

您将不得不在堆上分配它new[](并delete[]用于解除分配).

所以,你可以像这样创建数组:

// Create the array
int ** myArray = new int*[512];
for(int i=0; i<512; i++)
    myArray[i] = new int [512];

myArray[12][64] = 52; // Work with the array as you like

// Destroy the array
for(int i = 0 ; i < 512 ; i++ )
    delete [] myArray[i];
delete [] myArray;
Run Code Online (Sandbox Code Playgroud)