c++ - 超出内存或无效内存引用 - C++ 错误

标签 c++ algorithm memory flood-fill

有人可以阐明为什么我使用以下代码会收到以下错误吗?

Execution error: Your program had this runtime error:
        Exceeded memory or invalid memory reference. The program ran for
        0.000 CPU seconds before the error. It used 3048 KB of memory. 
    ------ Data ------
    7 4 
    11 6 11 6 3 10 6 
    7 9 6 13 5 15 5 
    1 10 12 7 13 7 5 
    13 11 10 8 10 12 13 
    ----------------------------

代码:

#include <iostream>
#include <fstream>
#include <string.h>
using namespace std;
int castle[50][50];
int components[50][50];
bool visited[50][50];
int N, M;

void flood(int y, int x, int component) {
    if(visited[y][x]) return;
    visited[y][x] = true;
    components[y][x] = component;
    if(x>0 && !((castle[y][x] & 1) == 1)) { //West
        cout<<"castle["<<y<<"]["<<x<<"] just";
        cout<<"flooded west\n";
        flood(y, x-1, component);
    }
    if(y>0 && !((castle[y][x] & 2) == 2)) { //North
        cout<<"castle["<<y<<"]["<<x<<"] just";
        cout<<"flooded north\n";
        flood(y-1, x, component);
    }
    if(x<M && !((castle[y][x] & 4) == 4)) { //East
        cout<<"castle["<<y<<"]["<<x<<"] just";
        cout<<"flooded east\n";
        flood(y, x+1, component);
    }
    if(y<N && !((castle[y][x] & 8) == 8)) { //South
        cout<<"castle["<<y<<"]["<<x<<"] just";
        cout<<"flooded south\n";
        flood(y+1, x, component);
    }
}

int main() {
    ifstream inp("castle.in");
    ofstream out("castle.out");
    memset(components, -2, sizeof(components));
    memset(visited, false, sizeof(visited));
    inp >> M >> N;
    for(int y=0; y<N; y++) {
        for(int x=0; x<M; x++) {
            inp >> castle[y][x];
        }
    }

    int curComponent = 0;
    for(int y=0; y<N; y++) {
        for(int x=0; x<M; x++) {
            if(visited[y][x]) continue;
            flood(y,x,++curComponent); //first component will be 1
//          cout << "curComponent = " << curComponent <<"\n";
        }
    }
    int roomAreas[curComponent+2]; //need an extra one since curComponent actually starts at 1
    memset(roomAreas, 0, sizeof(roomAreas));
    int greatestArea = 0;
    for(int y=0; y<N; y++) {
        for(int x=0; x<M; x++) {
            cout << "components[" << y <<"][" << x <<"] = " << components[y][x] << "\n";
            roomAreas[components[y][x]]++;
            if(roomAreas[components[y][x]] > greatestArea) greatestArea = roomAreas[components[y][x]];
        }
    }
    int greatestCombined = 0, bestx=0, besty=0;
    char bestDir='N'; //Means none -> error
    for(int y=0; y<N; y++) {
        for(int x=0; x<M; x++) {
        /*
            if(x>0 && components[y][x] != components[y][x-1] && roomAreas[components[y][x]] + roomAreas[components[y][x-1]] > greatestCombined) { //West
                greatestCombined = roomAreas[components[y][x]] + roomAreas[components[y][x-1]];
                bestx = x;
                besty = y;
                bestDir = 'W';
            }   
        */
            if(y>0 && components[y][x] != components[y-1][x] && roomAreas[components[y][x]] + roomAreas[components[y-1][x]] > greatestCombined) { //North
                greatestCombined = roomAreas[components[y][x]] + roomAreas[components[y-1][x]];
                bestx = x;
                besty = y;
                bestDir = 'N';
            }   
            if(x<M && components[y][x] != components[y][x+1] && roomAreas[components[y][x]] + roomAreas[components[y][x+1]] > greatestCombined) { //East
                greatestCombined = roomAreas[components[y][x]] + roomAreas[components[y][x+1]];
                bestx = x;
                besty = y;
                bestDir = 'E';
            }
        /*          
            if(y<N && components[y][x] != components[y+1][x] && roomAreas[components[y][x]] + roomAreas[components[y+1][x]] > greatestCombined) { //South
                greatestCombined = roomAreas[components[y][x]] + roomAreas[components[y+1][x]];
                bestx = x;
                besty = y;
                bestDir = 'S';
            }   
        */
        }
    }
    out << curComponent << "\n" << greatestArea << "\n" << greatestCombined << "\n" << besty+1 << " " << bestx+1 << " " << bestDir << "\n";
    return 0;
}

非常感谢!

最佳答案

可能

if(x<M && ...

应该是

if(x<M-1 && ...

因为在那部分,它可以递归地泛洪(x+1)。这可能意味着传入等于 M 的 x,它溢出了东边的边界。

同样适用于 y 洪水南方检查。

关于c++ - 超出内存或无效内存引用 - C++ 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5697613/

相关文章:

python - 寻找最繁忙时段的算法?

c++ - 传递参数时创建的对象应该在哪里删除?

c - 如何使数据类型小于 32 位

c++ - C++ 链接是否足够聪明,可以避免链接未使用的库?

c - 单程飞行问题

c++ - 如何在 Windows 服务程序中捕获 SERVICE_CONTROL_SHUTDOWN 代码

C++ - 最小堆实现和后序遍历

c++ - AIX 中的编程内存监视

c++ - 正确地将二进制数据转换为 char * 类型

c++ - opencv Qt 如何有效地创建彩色图像并将其传递给 QImage?