簡體   English   中英

數組問題:變量“ arr”周圍的堆棧已損壞

[英]Array issues: Stack around the variable 'arr' was corrupted

我正在編寫一個請求數組的函數,如果它找到一個大寫字母,則應該將整個行交換為大寫字母。 否則,它只會打印功能。 可以將主要功能運行到該功能要檢查大寫字母的部分,然后出現標題中提到的錯誤。

主功能:

#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));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM