1

When I build the code, this run without problems, however I debuging the code, this generate the message: Function uses '1600000620' bytes of stack: exceeds/analyze:stacksize 16384'.

I put the declaration: int array[2000][2000] into int main{} because when int array[2000][2000] was out of int main{}, it generate the error: 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;
}

I expected that this program clear a image, take out noise from images.

Patrick Boni
  • 17
  • 1
  • 6
  • 3
    I'm pretty sure I know what's causing the "array is ambiguous" error. However it's not possible to state this with 100% certainty, so I won't, because your question fails to meet all requirements of a [mre], as explained in the [help]. This huge array is just small enough to fit on the stack during standalone execution, but the additional overhead of debugging blows it past your operating system's limit. Solution: don't put it on the stack. Or use a vector. It's not possible to determine anything else, because of the aforementioned failure to show a [mre]. For more help, see [ask] questions. – Sam Varshavchik Nov 05 '19 at 01:57
  • 3
    Given the rest of the function, the ambiguity is almost certainly because of `using namespace std;`. A prime reason not to use that is being unaware of what names it brings in and being confused by resulting conflicts. – chris Nov 05 '19 at 02:28
  • 3
    1600000620 bytes of stack space! Now that's Stack Overflow! Seriously, are you sure it wasn't 16000620 which would make more sense. – doug Nov 05 '19 at 02:49
  • Please edit your question to include the *complete* example. Certainly you have some includes and other stuff before `main`, right? As suggested by others already, the stuff before `main` *does* make a difference in the behavior of the rest of the program. – walnut Nov 05 '19 at 04:13
  • Right, there are also a function before int main. I am sorry, It was my fault. I stumbled with this error before, the trouble was with a Mat matrix. – Patrick Boni Nov 06 '19 at 03:05
  • 3
    `int arr[2000][2000];` adds `2000 * 2000 * sizeof (int)` on the stack... `16000000`. That's is where I would start, since that is roughly 4 times the normal stack space on Linux and 16 times the normal stack space on windows. – David C. Rankin Nov 06 '19 at 03:11

2 Answers2

1

You were unable to move the declaration int array[2000][2000]; into global scope, because you are using using namespace std;.

The using namespace std; statement instructs the compiler to import all names found in the namespace std into the global namespace, so that you can use them directly (e.g. string) instead of accessing them through their namespace (e.g. std::string).

This is often discouraged, see this question, and you have a perfect example of why here.

Since C++11, there is a class template named array in the standard library namespace std, see reference for std::array.

That means array already may have a meaning in global scope. Then you try to declare a variable with the name array and the compiler doesn't know anymore whether array is supposed to refer to the variable you declared or the class template imported from the standard library namespace. Thus the error message.

To resolve this, use a different name for your variable array, or better, don't use using namespace std; and qualify all your references to the standard library with std::.

walnut
  • 21,629
  • 4
  • 23
  • 59
1

Change the name of the variable array, and put it out of int main{}, before int main{} solve the problem. The ambiguous was caused because array is a class in C++, a and the code before it was being stated how a integer. So change the name array to another any name like arr1 solve the problem.

#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;
}
Patrick Boni
  • 17
  • 1
  • 6