正常块后检测到堆损坏 (#181) [已关闭]

heap corruption detected after normal block (#181) [closed]

提问人:Ali Samie 提问时间:11/5/2016 最后编辑:trincotAli Samie 更新时间:11/8/2021 访问量:805

问:


编辑问题以包括所需的行为、特定问题或错误以及重现问题所需的最短代码。这将有助于其他人回答这个问题。

7年前关闭。

(首先对不起英语,因为我不擅长)

我已经为magic-square编写了一个代码,该代码工作正常并给出了真正的正方形。

当它被编译并运行程序时,没有任何问题。

但是当它到达 我得到这个错误return 0main()

正常块后检测到堆损坏 (#180)

我搜索了一下,发现我的班级没有遵循三法则(什么是三法则?square)

所以我试了一下,又得到了错误。

这是我的代码。

#include<iostream>


using namespace std;

class square {

public:
//a n*n square which n=size
    int size;
//a set of blocks in square which is declared
//by pointers cuz i couldn't simply write block[size][size]
    int** block = new int*[size];
    square(const int _size) {
        size = _size;
        for (int c = 0;c < size;c++) {
            block[c] = new int[size];
        }
//putting 0 value in all blocks
        for (int i = 0; i < size;i++)
            for (int j = 0;j < size;j++) {
                block[i][j] = 0;
            }
    }
//distructor
    ~square() {
        for (int c = 0; c < size; c++)
            delete block[c];

        delete block;
    }
//copy constructor
    square(const square& that) {
        for (int c = 0; c < size; c++)
            delete block[c];

        delete block;
        size = that.size;
        block = new int*[size];
        for (int c = 0;c < size;c++) {
            block[c] = new int[size];
        }
        for (int i = 0; i < size;i++)
            for (int j = 0;j < size;j++) {
                block[i][j] = that.block[i][j];
            }
    }
//add or change the value of a block
    void add_block(int &value, int i, int j) {
        block[i][j] = value;
    }
//get the value stored in a block
    int get_block(int i, int j) {
        return block[i][j];
    }
//get the size of the square
    int get_size() {
        return size;
    }
//display the square
    void disp() {
        for (int i = 0; i < size;i++) {
            for (int j = 0;j < size;j++) {
                cout << block[i][j]<<' ';
            }
            cout << endl;
        }

    }


};
//just to write clean code i made this function
//to get the size of the square from input
int intro() {
    cout << "magic square" << endl;
    cout << "what's the size of rows and columns?(just enter an integer and must be one of the following numbers)" << endl;
    cout << "3 , 5 , 7 , ... , (2n+1)" << endl;
    int s_size;
    cin >> s_size;
    while (s_size % 2 == 0) {
        cout << "!!! must be one of the following numbers : 3 , 5 , 7 , ... , (2n+1) !!!" << endl;
        cout << "enter again" << endl;
        cin >> s_size;
    }
    return s_size;
}
//look for next valid block to add the next number
//and adding the next number
//the algorithm is go up.go left.if not filled before
//put new number. else go back to last position.go down.put the number.
void add_next_magic_block(square &s,int &v,int &i , int &j) {
    int s_size = s.get_size();
    int ii = i;
    int jj = j;
    if (i == 0)
        i = s_size - 1;
    else
        i--;
    if (j == s_size - 1)
        j = 0;
    else
        j++;
    if (s.get_block(i, j) == 0)
        s.add_block(v, i, j);
    else {
        s.add_block(v, ++ii, jj);
        i = ii;
        j = jj;
    }
}
//magic square which starts from [0][j/2]
//and ends when it reaches to the 
//size*size 
//which is the last number
void do_magic_square_on(square &s) {
    int end = s.get_size()*s.get_size();
    int count=1;
    int i = 0;
    int j = s.get_size() / 2;
    s.add_block(count, 0, j);
    count++;

    while (count <= end) {
        add_next_magic_block(s, count, i, j);
        count++;
    }
}


int main() {
    int s_size = intro();
    square my_s(s_size);

    do_magic_square_on(my_s);

    my_s.disp();
    system("pause");
    return 0;
}

不是关于问答 这是编辑后的代码 下面是运行新代码 http://cpp.sh/72mmg 的链接

#include<iostream>


using namespace std;

class magic_square {

public:


    int size;
    int** block;
    magic_square(int _size) :size(_size) , block(new int*[size]) {
        size = _size;
        for (int c = 0;c < size;c++) {
            block[c] = new int[size];
        }
        for (int i = 0; i < size;i++)
            for (int j = 0;j < size;j++) {
                block[i][j] = 0;
            }
    }

    ~magic_square() {
        for (int c = 0; c < size; c++)
            delete block[c];

        delete block;
    }

    magic_square(const magic_square& that) {
        for (int c = 0; c < size; c++)
            delete block[c];

        delete block;
        size = that.size;
        block = new int*[size];
        for (int c = 0;c < size;c++) {
            block[c] = new int[size];
        }
        for (int i = 0; i < size;i++)
            for (int j = 0;j < size;j++) {
                block[i][j] = that.block[i][j];
            }
    }

    void add_block(int &value, int i, int j) {
        block[i][j] = value;
    }

    int get_block(int i, int j) {
        return block[i][j];
    }

    int get_size() {
        return size;
    }

    int sum_of_row(int row) {
        int sum = 0;
        int c = 0;
        for (int c = 0; c < this->get_size(); c++)
            sum += this->get_block(row, c);
        return sum;
    }

    int sum_of_col(int col) {
        int sum = 0;
        int c = 0;
        for (int c = 0; c < this->get_size(); c++)
            sum += this->get_block(c, col);
        return sum;
    }

    void disp() {
        for (int i = 0; i < size;i++)
            cout << '\t' << "["<<i+1<<"]";
        cout <<'\t'<<"row sum"<< endl << endl;
        for (int i = 0; i < size;i++) {
            cout << "[" << i + 1 << "]";
            for (int j = 0;j < size;j++) {
                cout<<'\t'<< block[i][j];
            }
            cout <<'\t'<<sum_of_row(i) <<endl;
        }
        cout <<endl <<"col sum";
        for (int i = 0; i < size;i++)
            cout << '\t'<< sum_of_col(i);
        cout << endl;
    }


};

int intro() {
    cout << "magic square" << endl;
    cout << "what's the size of rows and columns?(just enter an integer and must be one of the following numbers)" << endl;
    cout << "3 , 5 , 7 , ... , (2n+1)" << endl;
    int s_size;
    cin >> s_size;
    while (s_size % 2 == 0) {
        cout << "!!! must be one of the following numbers : 3 , 5 , 7 , ... , (2n+1) !!!" << endl;
        cout << "enter again" << endl;
        cin >> s_size;
    }
    return s_size;
}

void add_next_magic_block(magic_square &s,int &v,int &i , int &j) {
    int s_size = s.get_size();
    int ii = i;
    int jj = j;
    if (i == 0)
        i = s_size - 1;
    else
        i--;
    if (j == s_size - 1)
        j = 0;
    else
        j++;
    if (s.get_block(i, j) == 0)
        s.add_block(v, i, j);
    else {
        s.add_block(v, ++ii, jj);
        i = ii;
        j = jj;
    }
}

void normal_do_magic_square_on(magic_square &s) {
    int end = s.get_size()*s.get_size();
    int count = 1;
    int i = 0;
    int j = s.get_size() / 2;
    s.add_block(count, 0, j);

    //cout <<endl<< "   <------STEP------>  "<< "[" << count << "]" << endl<<endl;
    //s.disp();
    //cout << endl;

    count++;
    while (count <= end) {
        add_next_magic_block(s, count, i, j);

        //cout << endl << " <------STEP------>  " << "["<<count<<"]" << endl << endl;
        //s.disp();
        //cout << endl;

        count++;
    }
}

void step_do_magic_square_on(magic_square &s) {
    int end = s.get_size()*s.get_size();
    int count=1;
    int i = 0;
    int j = s.get_size() / 2;
    s.add_block(count, 0, j);

    cout <<endl<< " <------STEP------>  "<< "[" << count << "]" << endl<<endl;
    s.disp();
    cout << endl;

    count++;
    while (count <= end) {
        add_next_magic_block(s, count, i, j);

        cout << endl << "   <------STEP------>  " << "["<<count<<"]" << endl << endl;
        s.disp();
        cout << endl;

        count++;
    }
}

//to decide if show the steps or no
void out_put(magic_square &s) {
    cout << "want to see the steps?(y,n)";
    char in;
    cin >> in;
    if (in == 'n' || in == 'N') {
        normal_do_magic_square_on(s);
        s.disp();
    }

    else if(in=='y' || in=='Y')
        step_do_magic_square_on(s);
}

int main() {
    int s_size = intro();
    magic_square my_s(s_size);
    out_put(my_s);
    system("pause");
    return 0;
}
C++ 内存堆 损坏 三法则

评论

0赞 Baum mit Augen 11/5/2016
编辑您的问题,以提供一个最小的可重复示例
0赞 Ali Samie 11/5/2016
@BaummitAugen 我不知道你说的完全是什么意思。它已经完成了。
3赞 Baum mit Augen 11/5/2016
虽然不是很小。
0赞 greybeard 11/5/2016
请注释您的代码。什么是下一个(/上一个),为什么是?s.add_block(v, ++ii, jj);
0赞 Ali Samie 11/5/2016
@greybeard我使用一种以这种方式工作的算法。first : 将第一个数字放在 [0][j/2] 的位置。然后是 [I-1][J+1] 位置的下一个数字。因此,如果下一个方块在之前被填满,则将数字放在 [i+1][j] 的位置并继续直到正方形已满

答:

0赞 Pedro Boechat 11/5/2016 #1

实际上,当您尝试为分配内存时,会发生错误,此处

int** block = new int*[size];

这是因为成员变量大小在代码的此时未初始化,并且包含垃圾。我的建议是在构造函数中初始化两个变量,如下所示:

int size;
int** block
square(int _size) : size(_size), block(new int*[size]) {
    size = _size;
    for (int c = 0; c < size; c++) {
        block[c] = new int[size];
    }
    for (int i = 0; i < size; i++)
        for (int j = 0; j < size; j++) {
            block[i][j] = 0;
        }
}

请注意,因为在上面的代码中我决定初始化 size,然后使用它来初始化块,所以必须在之前定义 size如果将声明的顺序更改为类似

int** block
int size;

代码将再次使用未初始化的大小

评论

0赞 Ali Samie 11/5/2016
我认为你是对的,但你确定语法吗?当我在IDE中编写它时,它告诉我语法有问题。
0赞 Pedro Boechat 11/5/2016
是的,看看吧:cpp.sh/8mcvr
0赞 Ali Samie 11/5/2016
我检查了链接,它工作正常,但在复制您的代码后再次出现错误。
0赞 Pedro Boechat 11/5/2016
你用的是什么编译器?错误发生在哪里?
0赞 Ali Samie 11/5/2016
我使用 Visual Studio 2015...当它达到return 0main