为什么不能成功分配数组?

时间:2018-08-16 09:56:42

标签: c++ arrays dynamic-memory-allocation

这是我的代码:

#include <iostream>
#include <stdlib.h>
using namespace std;

int find_cross(char*, int, int, int);
void reduction(char*, char*, int, int);

int main(){
    int n;
    int m;
    cin >> n >> m;
    char *data = new char[n * m];
    for(int i = 0; i < n; i++){
        cin >> (data+i*m);
    }
    cout << "pass one" << endl;
    char *original = new char[n * m];
    cout << "pass two" << endl;
    for(int i = 0; i < n * m; i++){
        *(original+i) = *(data+i);
    }
    int biggest_size;
    if(n >= m){
        if(m % 2 == 0){
            biggest_size = m - 1;
        }else{
            biggest_size = m;
        }
    }else{
        if(n % 2 == 0){
            biggest_size = n - 1;
        }else{
            biggest_size = n;
        }
    }
    {
        int biggest = 0;
        for(int i = 1; i < biggest_size + 1; i = i + 2){
            reduction(data, original, n, m);
            if(find_cross(data, i, n, m)){
                for(int j = biggest_size; j >= 1; j = j - 2){
                    if(find_cross(data, j, n, m)){
                        if((i * 2 - 1) * (j * 2 - 1) > biggest){
                            biggest = (i * 2 - 1) * (j * 2 - 1);
                        }
                        break;
                    }
                }
            }
        }
        cout << biggest;
    }
    delete [] data;
    delete [] original;
    system("pause");
    return 0;
}

int find_cross(char* data, int size, int row, int column){
    for(int i = (size - 1) / 2; i < row - (size - 1) / 2; i++){
        for(int j = (size - 1) / 2; j < column - (size - 1) / 2; j++){
            int yes = 1;
            for(int k = 0; k < (size - 1) / 2 + 1; k++){
                if(*(data+(i+k)*column+j) != 'G' || *(data+(i-k)*column+j) != 'G' || *(data+i*column+j+k) != 'G' || *(data+i*column+j-k) != 'G'){
                    yes = 0;
                    break;
                }
            }
            if(yes == 1){
                for(int k = 0; k < (size - 1) / 2 + 1; k++){
                    *(data+(i-k)*column+j) = 'A';
                    *(data+(i+k)*column+j) = 'A';
                    *(data+i*column+j+k) = 'A';
                    *(data+i*column+j-k) = 'A';
                }
                return 1;
            }else if(i == row - (size - 1) / 2 - 1 && j == column - (size - 1) / 2 - 1){
                return 0;
            }
        }
    }
    return -1;
}

void reduction(char* data, char* original, int row, int column){
    for(int i = 0; i < row; i++){
        for(int j = 0; j < column; j++){
            *(data+i*column+j) = *(original+i*column+j);
        }
    }
}

这是我的输入内容

14 12
GGGGGGGGGGGG
GGGGGGGGGGGG
BGBGGGBGBGBG
BGBGGGBGBGBG
GGGGGGGGGGGG
GGGGGGGGGGGG
GGGGGGGGGGGG
GGGGGGGGGGGG
BGBGGGBGBGBG
BGBGGGBGBGBG
BGBGGGBGBGBG
BGBGGGBGBGBG
GGGGGGGGGGGG
GGGGGGGGGGGG

执行结果:

14 12
GGGGGGGGGGGG
GGGGGGGGGGGG
BGBGGGBGBGBG
BGBGGGBGBGBG
GGGGGGGGGGGG
GGGGGGGGGGGG
GGGGGGGGGGGG
GGGGGGGGGGGG
BGBGGGBGBGBG
BGBGGGBGBGBG
BGBGGGBGBGBG
BGBGGGBGBGBG
GGGGGGGGGGGG
GGGGGGGGGGGG
pass one

--------------------------------
Process exited after 256.8 seconds with return value 3221226356

问题发生在char *original = new char[n * m];,无法成功执行,并且程序崩溃。但是发生了一件奇怪的事,可以成功分配数组char *data = new char[n * m];。发生了什么,如何解决这个问题? Original picture

1 个答案:

答案 0 :(得分:4)

您忘记了在读取字符串时添加的nul终止符。您的第二次分配失败,因为您已超出写入范围,从而破坏了堆。您需要在数组中再增加一个字节

char *data = new char[n * m + 1];

或者您可以做一些明智的事情,并将其视为学习的教训,并使用字符串向量。

vector<string> data(n);
for(int i = 0; i < n; i++){
    cin >> data[i];
}
vector<string> original = data;

看看您的代码变得更加清晰和容易了吗?