如何修复字符数组变量的缓冲区溢出?

问题描述 投票:0回答:1

我从 pword char 数组中收到运行时错误代码 #2 缓冲区溢出。任何有助于解决问题的提示。

//Write a program to determine whether or not an entered 
//password is valid.Valid passwords consist of 10 characters,
//4 of which must be letters and the other 6 digits.
//The letters and digits can be arranged in any order.
//Only lowercase letters are allowed for valid passwords.
//Hints: use islower, isalpha, isdigit, and strlen.

#include<iostream>
#include<cctype>
#include<string>

using namespace std;

int main()
{
    const int Size = 10;
    int pword[Size];
    int count=0;
    int totall = 0;
    int totaln = 0;
    bool state = NULL;

    while(state != true) 
    {
    
        cout << "Enter a password up to 10 characters: " << endl;
        cin >> pword;

        int psize = strlen(pword);
        totall = 0;
        totaln = 0;
        while (pword[count] != '\0')

        {
            if (isalpha(pword[count]))
            {
                totall += 1;
            }

            if (isdigit(pword[count]))
            {
                totaln += 1;
            }
            count++;
        }
        if (totall > 4 || totaln > 6)
        {
            cout << " Error! Only enter 4 characters and 6 digits." << endl;
            state = false;
        }
        else
        {
            state = true;
        }
        count = 0;
        while(pword[count]!='\0')
        {
            if (!islower(pword[count]))
            {
                if (!isdigit(pword[count]))
                {
                    cout << "Error all characters must be lowercase!" << endl;
                    state = false;
                }
            }  
            count++;
        }

     
    }
   
        cout << "The password you entered is: " << pword << endl;
        cout << "The number of lower case letters in the password is " << totall << endl;
        cout << "The number of digits in the password is " << totaln << endl;
      
        return 0;
}

我尝试通过以下方式动态分配字符数组:

pword[Size]= (char*)malloc(sizeof(char))={};

但是失败了,所以我恢复了代码。我试图在堆上为 char 数组索引 0-10 创建一个空间,并在索引 10 处使用 null 终止字符。看看 malloc,虽然我不明白如何通过这种方式使数组比 char 更长?

c++ arrays char runtime-error stack-overflow
1个回答
0
投票

主要修复是更改此声明:

int pword[Size];

简单来说:

string pword;

当您输入超过 9 个字符时,不再有缓冲区溢出的可能性。

代码的一些小改进:

int main()
{
    const int Size = 10;
    string pword;
    int totall = 0;
    int totaln = 0;
    int isAllLowercase = true;
    bool state = false;

    while(state != true) 
    {
        cout << "Enter a password up to 10 characters: " << endl;
        cin >> pword;

        int psize = strlen(pword);
        totall = 0;
        totaln = 0;

        for (char c : pword)
        {
            if (isalpha(c))
            {
                totall += 1;
            }

            if (isdigit(c))
            {
                totaln += 1;
            }
            isAllLowerCase = isAllLowerCase && (islower(c) || isdigit(c));
        }
        if (totall > 4 || totaln > 6)
        {
            cout << " Error! Only enter 4 characters and 6 digits." << endl;
        }
        else if (!isAllLowerCase)
        {
            cout << "Error all characters must be lowercase!" << endl;
        }
        else
        {
            state = true;
        }
    }
   
    cout << "The password you entered is: " << pword << endl;
    cout << "The number of lower case letters in the password is " << totall << endl;
    cout << "The number of digits in the password is " << totaln << endl;
      
    return 0;
}
© www.soinside.com 2019 - 2024. All rights reserved.