使用类 - 无效的分配大小

时间:2014-09-03 17:12:49

标签: c++ arrays class

我有作业,但它不起作用。最好我能猜出为什么它不起作用是vs2013的自动断点,它在输出框中显示。

HEAP[hw2_ccc.exe]: Invalid allocation size - 10 (exceeded fffdefff)

First-chance exception at 0x000007FEFD9D940D in hw2_ccc.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x000000000012F350.

HEAP[hw2_ccc.exe]: Invalid allocation size - 1 (exceeded fffdefff)

First-chance exception at 0x000007FEFD9D940D in hw2_ccc.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x000000000012CE40.

First-chance exception at 0x000007FEFD9D940D in hw2_ccc.exe: Microsoft C++ exception: [rethrow] at memory location 0x0000000000000000.

First-chance exception at 0x000007FEFD9D940D in hw2_ccc.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x000000000012CE40.

A buffer overrun has occurred in hw2_ccc.exe which has corrupted the program's internal state.

这是我的代码:

bankAccount.h

#ifndef H_bankAccount
#define H_bankAccount

#include <string>
#include <iostream>

using namespace std;

class bankAccount
{
public:
    void setAccountName(string holderName);
    void setAccountNumber(int accountNumber);
    void setInterestRate(double interestRate);
    void setAccountType(string accountType);
    void depositBalance(double deposit);
    void withdrawBalance(double withdraw);
    void printAccountInformation();
    void newAccount(string holderName, int accountNumber, string accountType, double interestRate);
    string getAccountName();
    bankAccount();
    ~bankAccount();

private:
    string holderName;
    int accountNumber;
    string accountType;
    double balance;
    double interestRate;

};
#endif

bankAccount.cpp

#include "bankAccount.h"

using namespace std;

bankAccount::bankAccount()
{

}

bankAccount::~bankAccount()
{

}

void bankAccount::newAccount(string holderName, int accountNumber, string accountType, double interestRate)
{
    setAccountName(holderName);
    setAccountNumber(accountNumber);
    setAccountType(accountType);
    setInterestRate(interestRate);
}

void bankAccount::setAccountName(string holderName)
{
    bankAccount::holderName = holderName;
}

void bankAccount::setAccountNumber(int accountNumber)
{
    bankAccount::accountNumber = accountNumber;
}

void bankAccount::setAccountType(string accountType)
{
    bankAccount::accountType = accountType;
}

void bankAccount::setInterestRate(double interestRate)
{
    bankAccount::interestRate = interestRate;
}

void bankAccount::depositBalance(double deposit)
{
    bankAccount::balance += deposit;
}

void bankAccount::withdrawBalance(double withdraw)
{
    bankAccount::balance -= withdraw;
}

void bankAccount::printAccountInformation()
{
    cout << "Account Name: " << bankAccount::holderName << endl;
    cout << "Account Type: " << bankAccount::accountType << endl;
    cout << "Account Number: " << bankAccount::accountNumber << endl;
    cout << "Account Interest Rate: " << bankAccount::interestRate << endl;
    cout << "Account Balance :" << bankAccount::balance << endl;
}

string bankAccount::getAccountName()
{
    return holderName;
}

的main.cpp

#include "bankAccount.h"

using namespace std;

int randAccountNum();
string randAccountType();
int interestRate(string accountType);
bool printAccount(bankAccount accounts[10]);

int main()
{
    bankAccount account[10];
    string accountNames[10] = { "Bob", "Jack", "Billy", "James", "Kathy", "John", "Jenny", "Penny", "Sue", "Louis" };
    string accountType;
    int accountNumber;
    bool prAcc = true;

    for (int i = 0; i < sizeof(account); i++)
    {
        accountType = randAccountType();
        accountNumber = randAccountNum();
        account[i].newAccount(accountNames[i], accountNumber, accountType, interestRate(accountType));
    }

    while (prAcc)
    {
        prAcc = printAccount(account);
    }

    system("pause");

    return 0;
}

int randAccountNum()
{
    int num = rand() % 1000 + 1;
    return num;
}

string randAccountType()
{
    string str;
    int num = rand() % 2 + 1;
    if (num = 1)
    {
        str = "Savings";
    }
    else {
        str = "Checking";
    }

    return str;
}

int interestRate(string accountType)
{
    int ir;

    if (accountType == "Savings")
    {
        ir = 2;
    }
    else {
        ir = 4;
    }

    return ir;
}    

bool printAccount(bankAccount accounts[10])
{
    string cont;
    bool contL = true;
    string accountName;

    cout << "Enter account name: ";
    cin >> accountName;
    cout << endl;

    for (int i = 0; i < sizeof(accounts); i++)
    {
        if (accounts[i].getAccountName() == accountName)
        {
            accounts[i].printAccountInformation();
        }
    }




    while (contL)
    {
        cout << "Enter another name? (Yes/No): ";
        cin >> cont;
        if (cont == "Yes")
            return true;
        else if (cont == "No")
            return false;
        else
            cout << "Invalid. Please enter Yes or No" << endl;
    }


}

2 个答案:

答案 0 :(得分:4)

更改此声明

for (int i = 0; i < sizeof(account); i++)

for (int i = 0; i < sizeof(account) / sizeof(*account); i++)

此外还有功能错误

bool printAccount(bankAccount accounts [10]);

在本声明中

for (int i = 0; i < sizeof(accounts); i++)

sizeof(accounts)等于sizeof(bankAccount *)

您必须为传递给函数的数组的大小声明第二个参数。

考虑到您可以使用std::array类型的对象而不是数组。在这种情况下,阵列的大小不会出现这样的问题。

答案 1 :(得分:2)

在您的代码中:

bankAccount account[10];

...

for (int i = 0; i < sizeof(account); i++)

循环迭代中存在问题(特别是在上限中)。

您的account数组包含10个bankAccount类的实例 很明显,您的意图是遍历所有数组项。为了使其正常工作,i循环索引的有效值为0,1,2,3,...,9。因此,如果sizeof(account)返回10,这将起作用(事实上,你使用<运算符,排除10的上限,然后在9之前停止一个值,这是正确的。)

问题是sizeof(account)确实返回account数组的字节数。这相当于10 * sizeof(bankAccount),其中sizeof(bankAccount)再次返回bankAccount类的字节大小。假设您的bankAccount班级&#39;大小是例如40个字节,然后sizeof(account)变为10 * 40,即400个字节。

因此,在这种情况下,在您的for循环中,您的索引跨越值 0,1,2,3,4,...9, 10,11,12,...399粗体是有效的,接下来的不是!

你有一个所谓的缓冲区溢出,这在VC ++编译器的错误信息中有明确说明:

[...] A buffer overrun has occurred

要解决这个问题,您有几种选择。

例如,要获得正确的项目计数,您可以将数组的大小(以字节为单位)除以其元素的一个的大小,例如第一个,在索引0:

// Count of items in the 'account' array
const int accountCount = sizeof(account) / sizeof(account[0]);

for (int i = 0; i < accountCount; i++)
    ...

另一个选择是使用更现代的C ++方法,例如使用C ++ 11&#39; std::array

#include <array>   // for std::array
...

// Define an array of 10 bankAccount's
std::array<bankAccount, 10> account;

// Note that account.size() is 10!
// i.e., this is the size in *element count* (not in bytes, like sizeof()).
for (int i = 0; i < account.size(); i++)
    ...