数组问题:变量周围的堆栈'arr'已损坏

Array issues: Stack around the variable 'arr' was corrupted

本文关键字:arr 已损坏 堆栈 问题 变量 周围 数组      更新时间:2023-10-16

我正在编写一个请求数组的函数的中间,如果找到大写字母,则应该将整个行交换为Captial字母。否则,它只是打印功能。运行主函数直到可以检查Captial Letters的函数的部分是可以的,我会在标题中提到的错误。

主要功能:

#include <iostream>
#include "FuncA.h"
#include "printarr.h"
using namespace std;
void main()
{
    char choice;
    do
    {
        cout << "Welcome, choose a function to continue: " << endl << "n A for Uppercasing arrays. n B for Column sum. n C for String copying. n D for exit" << endl;
        cin >> choice;
        switch (choice)

            case 'A':
                    funca();
        } 
        while (choice != 'D');
    }

和所讨论的功能:

#include <iostream>
#include "FuncA.h"
#include "printarr.h"
using namespace std;
void funca()
{
    int  rows = 0, cols = 0; //init
    cout << "how many rows? ";
    cin >> rows;
    cout << "n how many cols? ";
    cin >> cols;
    char arr[][COLS] = {0};

    for (int i = 0; i < cols; i++) // input
    {
        for (int j = 0; j < rows; j++)
        {
            cin >> arr[i][j];
        }
    }
    for (int i2 = 0; i2 < cols; i2++) // capcheck and printing if caps not detected
    {
        for (int j2 = 0; j2 < rows; j2++)
        {
            if (arr[i2][j2] >= 90 || arr[i2][j2] <= 65)
            {
                printarr(arr, rows, cols);
            }
        }
    }

}

如何解决此问题?我尝试更改COLS的大小(在.H文件中定义了大小(,但这无效。

您对arr的声明等于char arr[1][COLS]。第一个"维度"的任何非零索引都将超出范围。

如果您想要一个在运行时设置的"数组",则使用std::vector

std::vector<std::vector<char>> arr(cols, std::vector<char>(rows));