c++ - 堆栈周围的变量损坏加上程序在输入特定值c++后停止

标签 c++

我的程序是为了生成一个动态二维数组然后排序然后转置数组(切换列和行)我的问题是当我为行和列输入某个值(7)时变量索引周围的堆栈变得损坏此外,我的代码开始生成不可以的数字,因为我认为它是因为它有些超出范围请帮助我,我是 c++ 的相对新手


//
// C++ program template
//

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;
void init_array(int ** array_in, int rows, int cols, int list[]);
void print_array(int ** array_in, int rows, int cols);
void selection_sort(int array_in[], int elements);
int ** transpose(int ** array_in, int ** array_out, int rows, int cols);
const unsigned int SIZE = 4000;
int count1 = 0;
int main(void)
{
    int rows = 0, cols = 0, j = 0, k = 0;

    int**numbers = nullptr;
    int**arraytranspose = nullptr;
    cout << "Enter rows and columns" << endl;
    cin >> rows >> cols;
    int length = rows * cols;


    int list[4000] = { 0 };
    numbers = new int*[rows];
    arraytranspose = new int*[rows];

        for (k = 0; k < cols; k++)
    {
        numbers[k] = new int[cols];
        arraytranspose[k] = new int[cols];
    }
    // initialize the array with unique values
    init_array(numbers, rows, cols, list);
    print_array(numbers, rows, cols);

    selection_sort(list, count1);
    int count3 = 0;

    for (int count2 = 0; count2 < 3999; count2++) {
        for (int i = 0; i < rows; i++)
        {
            for (int c = 0; c < cols; c++)
            {

                if (list[count2] != 0)
                {

                    numbers[i][c] = list[count2];

                }
                count2++;
            }
        }
    }
    print_array(numbers, rows, cols);
    cout << endl << endl;

    print_array(transpose(numbers,arraytranspose,rows,cols), rows, cols);
    system("pause");
    return 0;
}

void selection_sort(int array_in[], int elements)
{
    int index = 0, smallest = 0, hold = 0, count = 0, location = 0;

    for (index = 0; index < SIZE - 1; index++) // Loop to control number of passes
    {
        smallest = index;
        // Find the index of the smallest element
        for (location = index + 1; location < SIZE; location++)
        {
            if (array_in[location] < array_in[smallest])
            {
                smallest = location;
            } // End If

        } // End Inner for loop

        hold = array_in[smallest];
        array_in[smallest] = array_in[index];
        array_in[index] = hold;
        count++; // Count number of swaps
    }
    cout << "There were " << count << " element exchanges during the sort. " << endl << endl;
    return;
}
void init_array(int ** array_in, int rows, int cols, int list[])
{
    int j = 0, k = 0, value = 0;
    int indices[4000] = { 0 };
    count1 = 0;

    while (j < rows)
    {
        k = 0;
        while (k < cols)
        {
            value = rand() & 4000;
            if (indices[value] != 1)
            {
                array_in[j][k] = value;
                indices[value] = 1;
                list[count1] = array_in[j][k];
                k++;
                count1++;
            }

        }// end while
        j++;
    }
    return;
}
void print_array(int ** array_in, int rows, int cols)
{
    int j = 0, k = 0;
    for (j = 0; j < rows; j++) {
        for (k = 0; k < cols; k++) {
            cout << setw(5) << array_in[j][k];
        }
        cout << endl;
    }
    return;
}
int** transpose(int ** array_in, int ** array_out,int rows, int cols)
{
    for (int r = 0; r < rows; r++)
    {
        for (int c = 0; c < cols; c++)
        {
            array_out[r][c] = array_in[c][r];
        }
    }

    return array_out;
}

最佳答案

numbers = new int*[rows];
arraytranspose = new int*[rows];

这为一对数组分配内存,一个值的数组。

紧接着:

for (k = 0; k < cols; k++)
{
    numbers[k] = new int[cols];
    arraytranspose[k] = new int[cols];
}

这会设置这些数组中的第一个 cols 值,但它们的大小是 rows 值。因此,如果 rows 小于 cols,这会导致内存损坏和未定义的行为,因为显示的代码写入了不存在的数组值。

这是所示代码中的第一个明显的缺陷,粗略检查后很明显,但很可能还有其他类似的缺陷;它们通常是由不安全的编程实践引起的,例如此处显示的那些,例如手动内存分配和缺乏边界检查。现代 C++ 代码提供了大量安全的编程实践,例如使用 std::vector 来管理动态大小的数组和迭代器。

简单地修复这个特定的错误将只是一个创可贴,即使它最终证明是唯一的错误修复。您真正的长期解决方案是重写整个代码,并开始使用现代 C++ 容器、容器和算法,如果使用得当,它们将消除出现此类错误的大部分机会。

关于c++ - 堆栈周围的变量损坏加上程序在输入特定值c++后停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59080919/

相关文章:

c++ - C++::在构造函数退出之前通过指针调用Member函数

c++递归没有明显原因退出

c++ - 为快速查找优化的有序关联数据结构?

c++ - 我似乎无法得到正确的平均值

c++ - 如何将 lib 文件和 header 添加到 C++ 项目

c++ - 为什么 ostream 不能转换为 ostream?

c++ - 我如何编写用于字符串的双哈希实现?

c++ - 线程基类 C++

C++ 对象引用/访问

C++:将字符串指针设置为常量字符串值