【问题标题】:This error:" C6262: Function uses '1600000620' bytes of stack: exceeds/analyze:stacksize '16384'. Consider moving some data to heap"此错误:“C6262:函数使用'1600000620'字节的堆栈:超出/分析:stacksize'16384'。考虑将一些数据移动到堆”
【发布时间】:2019-11-05 01:52:27
【问题描述】:

当我构建代码时,这个运行没有问题,但是我调试代码,这会生成消息:函数使用 '1600000620' bytes of stack: beyond/analyze:stacksize 16384'。

我将声明:int array[2000][2000] 放入 int main{},因为当 int array[2000][2000] 超出 int main{} 时,会生成错误:array is ambiguous。

#include <iostream> 
#include <fstream> 
#include <sstream> 
using namespace std;

/* Function to sort an array using insertion sort*/
void insertionSort(int arr[], int n)
{
    int i, key, j;
    for (i = 1; i < n; i++){
        key = arr[i];
        j = i - 1;
        /* Move elements of arr[0..i-1], that aregreater than key, to one 
position aheadof their current position */
        while (j >= 0 && arr[j] > key){
            arr[j + 1] = arr[j];
            j = j - 1;
        }
        arr[j + 1] = key;
    }
}

int arr[2000][2000];
int main()
{
    int array[2000][2000];
    int window[9], row = 0, col = 0, numrows = 0, numcols = 0, MAX = 0;
    ifstream infile("phone.jpg");
    stringstream ss;
    string inputLine = "";

    // First line : version
    getline(infile, inputLine);
    if (inputLine.compare("P2") != 0) cerr << "Version error" << endl;
    else cout << "Version : " << inputLine << endl;

    // Continue with a stringstream
    ss << infile.rdbuf();

    // Secondline : size of image
    ss >> numcols >> numrows >> MAX;

    //print total number of rows, columns and maximum intensity of image
    cout << numcols << " columns and " << numrows << " rows" << endl<< 
    "Maximium Intesity "<< MAX <<endl; 

    //Initialize a new array of same size of image with 0
    for (row = 0; row <= numrows; ++row)
    {
        array[row][0] = 0;
    }
    for (col = 0; col <= numcols; ++col) {
        array[0][col] = 0;
    }

    // Following lines : data
    for (row = 1; row <= numrows; ++row)
    {
        for (col = 1; col <= numcols; ++col)
        {
            //original data store in new array
            ss >> array[row][col];
        }
    }

    // Now print the array to see the result
    for (row = 1; row <= numrows; ++row)
    {
        for (col = 1; col <= numcols; ++col)
        {
        //neighbor pixel values are stored in window including this pixel
            window[0] = array[row - 1][col - 1];
            window[1] = array[row - 1][col];
            window[2] = array[row - 1][col + 1];
            window[3] = array[row][col - 1];
            window[4] = array[row][col];
            window[5] = array[row][col + 1];
            window[6] = array[row + 1][col - 1];
            window[7] = array[row + 1][col];
            window[8] = array[row + 1][col + 1];

            //sort window array
            insertionSort(window, 9);

            //put the median to the new array 
            arr[row][col] = window[4];
        }
    }

    ofstream outfile;

    //new file open to stroe the output image 
    outfile.open("Medianfilter.pnm");
    outfile << "P2" << endl;
    outfile << numcols << " " << numrows << endl;
    outfile << "255" << endl;

    for (row = 1; row <= numrows; ++row)
    {
        for (col = 1; col <= numcols; ++col)
        {
            //store resultant pixel values to the output file
            outfile << arr[row][col] << " ";
        }
    }

    outfile.close();
    infile.close();
    return 0;
}

我希望这个程序可以清除图像,去除图像中的噪点。

【问题讨论】:

  • 我很确定我知道是什么导致了“数组不明确”错误。但是,不可能 100% 肯定地说明这一点,所以我不会,因为您的问题不符合 minimal reproducible example 的所有要求,如 help center 中所述。这个巨大的数组足够小,可以在独立执行期间放入堆栈,但是调试的额外开销使其超出了操作系统的限制。解决方案:不要把它放在堆栈上。或者使用向量。由于上述未能显示minimal reproducible example,因此无法确定其他任何内容。如需更多帮助,请参阅How to Ask questions。
  • 考虑到函数的其余部分,几乎可以肯定是因为using namespace std;。不使用它的一个主要原因是不知道它会带来什么名称并且被由此产生的冲突所混淆。
  • 1600000620 字节的堆栈空间!现在是堆栈溢出!说真的,你确定不是 16000620 更有意义吗?
  • 请编辑您的问题以包含 complete 示例。当然,您在main 之前有一些包含和其他内容,对吧?正如其他人已经建议的那样,main 之前的内容确实会影响程序其余部分的行为。
  • int arr[2000][2000]; 在堆栈上添加2000 * 2000 * sizeof (int)...16000000。这就是我要开始的地方,因为这大约是 Linux 上正常堆栈空间的 4 倍和 Windows 上正常堆栈空间的 16 倍。

标签: c++


【解决方案1】:

您无法将声明 int array[2000][2000]; 移动到全局范围内,因为您使用的是 using namespace std;

using namespace std; 语句指示编译器将在命名空间std 中找到的所有名称导入全局命名空间,以便您可以直接使用它们(例如string)而不是通过它们的命名空间访问它们(例如@ 987654328@).

这通常是不鼓励的,请参阅this question,你有一个完美的例子来说明为什么。

从 C++11 开始,在标准库命名空间 std 中有一个名为 array 的类模板,参见 reference for std::array

这意味着array 已经可能在全球范围内具有意义。然后,您尝试声明一个名为 array 的变量,编译器不再知道 array 是否应该引用您声明的变量或从标准库命名空间导入的类模板。因此错误消息。

要解决此问题,请为变量使用不同的名称 array,或者更好的是,不要使用 using namespace std;,并使用 std:: 限定对标准库的所有引用。

【讨论】:

    【解决方案2】:

    更改变量数组的名称,将其移出 int main{},在 int main{} 解决问题之前。之所以产生歧义,是因为数组是 C++ 中的一个类,而在它之前的代码中,它是如何被声明为整数的。所以把 name 数组改成另一个任意名字,比如 arr1 就可以解决问题了。

    #include <iostream> 
    #include <fstream> 
    #include <sstream> 
    using namespace std;
    
    /* Function to sort an array using insertion sort*/
    void insertionSort(int arr[], int n)
    {
        int i, key, j;
        for (i = 1; i < n; i++){
            key = arr[i];
            j = i - 1;
            /* Move elements of arr[0..i-1], that aregreater than key, to one position 
     aheadof their current position */
            while (j >= 0 && arr[j] > key){
                arr[j + 1] = arr[j];
                j = j - 1;
            }
            arr[j + 1] = key;
        }
    }
    
    int arr[2000][2000];
    int arr1[2000][2000];
    int main()
    {
    
        int window[9], row = 0, col = 0, numrows = 0, numcols = 0, MAX = 0;
        ifstream infile("phone.pmg");
        stringstream ss;
        string inputLine = "";
    
        // First line : version
        getline(infile, inputLine);
        if (inputLine.compare("P2") != 0) cerr << "Version error" << endl;
        else cout << "Version : " << inputLine << endl;
    
        // Continue with a stringstream
        ss << infile.rdbuf();
    
        // Secondline : size of image
        ss >> numcols >> numrows >> MAX;
    
        //print total number of rows, columns and maximum intensity of image
        cout << numcols << " columns and " << numrows << " rows" << endl<< "Maximium 
        Intesity "<< MAX <<endl; 
    
        //Initialize a new array of same size of image with 0
        for (row = 0; row <= numrows; ++row)
        {
            arr1[row][0] = 0;
        }
        for (col = 0; col <= numcols; ++col) {
            arr1[0][col] = 0;
        }
    
        // Following lines : data
        for (row = 1; row <= numrows; ++row)
        {
            for (col = 1; col <= numcols; ++col)
            {
                //original data store in new array
                ss >> arr1[row][col];
            }
        }
    
        // Now print the array to see the result
        for (row = 1; row <= numrows; ++row)
        {
            for (col = 1; col <= numcols; ++col)
            {
                //neighbor pixel values are stored in window including this pixel
                window[0] = arr1[row - 1][col - 1];
                window[1] = arr1[row - 1][col];
                window[2] = arr1[row - 1][col + 1];
                window[3] = arr1[row][col - 1];
                window[4] = arr1[row][col];
                window[5] = arr1[row][col + 1];
                window[6] = arr1[row + 1][col - 1];
                window[7] = arr1[row + 1][col];
                window[8] = arr1[row + 1][col + 1];
    
                //sort window array
                insertionSort(window, 9);
    
                //put the median to the new array 
                arr[row][col] = window[4];
            }
        }
    
        ofstream outfile;
    
        //new file open to stroe the output image 
        outfile.open("Medianfilter.pmn ");
        outfile << "P2" << endl;
        outfile << numcols << " " << numrows << endl;
        outfile << "255" << endl;
    
        for (row = 1; row <= numrows; ++row)
        {
            for (col = 1; col <= numcols; ++col)
            {
                //store resultant pixel values to the output file
                outfile << arr[row][col] << " ";
            }
        }
    
        outfile.close();
        infile.close();
        return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 2021-12-07
      • 2017-05-11
      • 1970-01-01
      • 1970-01-01
      • 2019-01-12
      • 2019-09-23
      • 2021-01-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多