2014-09-03 90 views
1

我有作業,但它不工作。最好的我可以猜測,爲什麼它不工作是一個自動斷點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; 
    } 


} 
+0

ekhm ....'對(INT I = 0; I <的sizeof(賬戶);我++)' – 2014-09-03 17:13:47

+1

爲什麼不使用'的std :: VECTOR'。 ..? – crashmstr 2014-09-03 17:31:05

回答

2

在您的代碼:

bankAccount account[10]; 

... 

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

你在循環迭代的問題(特別是在上限)。

您的account數組包含您的bankAccount類的10個實例。
很明顯你的意圖是迭代所有的數組項。爲了正常工作,i循環索引的有效值爲0,1,2,3,...,9。所以,如果sizeof(account)返回10(實際上,您使用<運算符來排除10的上限,並且剛剛在9之前停止一個值,這是正確的),那麼這將工作。

問題是sizeof(account)確實會返回account數組的字節的大小。這相當於10 * sizeof(bankAccount)sizeof(bankAccount)再次返回bankAccount類的字節大小。假設你的bankAccount班的大小是例如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的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++) 
    ... 
+0

非常感謝。我接受了你使用現代方法的建議,並使用了這個數組。工作得很好。你以一種清晰的方式解釋,並發現。 – CodeMonkey 2014-09-03 21:34:59

+0

@ProSpartanAKACodeMonkey:不客氣。我很高興它有幫助。 – 2014-09-03 21:42:38

4

查NGE此語句

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

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

也有是在誤差函數

布爾printAccount(的BankAccount帳戶[10]);

在這份聲明中

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

的sizeof(賬戶)是等於sizeof(*的BankAccount)

您必須聲明第二個參數傳遞給函數的數組的大小。

考慮到您可以使用類型爲std::array的對象而不是數組。在這種情況下,陣列的大小不會出現這樣的問題。

+0

爲什麼不使用'account.size()'? – flakes 2014-09-03 17:19:50

+0

@Calpratt什麼是account.size()? – 2014-09-03 17:26:35

+0

等待不行,我很蠢。 正在考慮容器庫數組 – flakes 2014-09-03 17:30:10