ethan_enhe
ethan_enhe

Reputation: 9

an error occur when running a c++ script in cygwin

I tryed to run the following code in my cygwin

#include<iostream>
#include<cstdio>
using namespace std;
const int NR=2005;
int n,m;
bool arr[NR][NR];
int mx_jx(int x){
    int up[NR][NR],lf[NR][NR],rt[NR][NR];
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            up[i][j]=1,lf[i][j]=rt[i][j]=j;
    for(int i=1;i<=n;i++)
        for(int j=2;j<=m;j++)
            if(arr[i][j-1]==x)lf[i][j]=lf[i][j-1];
    for(int i=1;i<=n;i++)
        for(int j=m-1;j>=1;j--)
            if(arr[i][j+1]==x)rt[i][j]=rt[i][j+1];
    int ans=0;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++){
            if(arr[i][j]!=x)continue;
            if(i>1 && arr[i-1][j]==x){
                up[i][j]=arr[i][j-1]+1;
                lf[i][j]=max(lf[i][j],lf[i-1][j]);
                rt[i][j]=min(rt[i][j],rt[i-1][j]);
            }
            ans=max(ans,(rt[i][j]-lf[i][j]+1)*up[i][j]);
        }
    return ans;
}
int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++){
            int tmp;
            scanf("%d",&tmp);
            arr[i][j]=(int)tmp;
            if((i+j)&1)arr[i][j]=!arr[i][j];
        }

    printf("%d",max(mx_jx(0),mx_jx(1)));    
    return 0;
}

It passed the compile(0 warning,0 error), but when I tried to run it with the following input

4 4
0 1 1 1
1 1 1 0
0 1 1 0
1 1 0 1

It says "Command terminated"

these code could run correctly on other platform, how can I solve this problem on cygwin? thanks a lot

Upvotes: 0

Views: 39

Answers (2)

Yksisarvinen
Yksisarvinen

Reputation: 22394

While the answer from user3684240 is valid and it is definitely an issue with your code, more likely you have Stack Overflow due to allocating numerous huge arrays on stack and in global memory area.

You should use std::vector<std::vector<int> instead of plain C arrays.

std::vector<std::vector<int>> up(NR, std::vector<int>(NR, 0));
std::vector<std::vector<int>> lf(NR, std::vector<int>(NR, 0));
std::vector<std::vector<int>> rt(NR, std::vector<int>(NR, 0));

It is not very well optimized, but it's simplest solution I can provide.


For that bool array though, std::vector might behave... strange. I cannot give 100% guarantee that it will work if you simply substitute bool[][] with std::vector<std::vector<bool>> without testing it properly, because std::vector<bool> is optimized for memory and it has certain quirks that one should be aware of when using it.

You can try and see if that array of bools in global memory works (after changing arrays in mx_jx to vectors), maybe it does and you don't have to change it. If it doesn't, you can try to change to vector of vectors, then it should work.

Upvotes: 2

user3684240
user3684240

Reputation: 1590

The mistake is in that line:

scanf("%d", &arr[i][j]);

%d reads ints, but your array is of type bool. This leads to a memory error.

An easy way to fix it would be:

int read_value;
scanf("%d", &read_value);
arr[i][j] = read_value;

As an additional hint: If you enable compiler warnings (-Wall -Wextra -pedantic), the compiler will tell you about those kinds of mistakes.

Upvotes: 0

Related Questions