Unhandled exception (arrays, constructor)

I get an error when running my program:

"Unhandled exception at 0x00d62f3b in test.exe: 0xC0000005: Access violation reading location 0xcdcdcded."

---
In main, an array of customers is setup:

customers = new Customer[size] //Value of size is given by user earlier

Below is the other relevant parts...
---


Account::Account()
{
this->accountNo = "";
this->balance = 0;
}

Account::Account(string accountNo, double balance)
{
this->accountNo = accountNo;
this->balance = balance;
}

-----

ForeignAccount::ForeignAccount()
:Account()
{
this->interest = 0;
}

ForeignAccount::ForeignAccount(string accountNo, double balance, double interest)
:Account(accountNo, balance)
{
this->interest = interest;
}


----

Customer::Customer()
{
this->name = "?";
this->numAccounts = 0;
const int MAXSIZE = 5;
ForeignAccount accounts[MAXSIZE];
}



void Customer::setAccount(string accountNo, double balance, double interest)
{
this->accounts[this->numAccounts] = Account(accountNo, balance, interest);
this->numAccounts++;
}

-----

My problem is with the last part, i e when filling up the default accounts objects with data using the constructor that takes accountNo, balance and interest as input.

THE ISSUES CAME WHEN I changed to using a static array in Customer::Customer() instead of a dynamic one. With the dynamic version of that constructor, the setAccount function (and the rest of the program as well) worked without any issues. But I need to use the static version due to other reasons, so here I am.

Can anyone see what is causing the error?
THE ISSUES CAME WHEN I changed to using a static array in Customer::Customer() instead of a dynamic one.


You're not using a 'static' array. You're using a local automatic array whose memory is reclaimed when you leave the constructor.
Thanks cire.

Well that would explain the memory access violation then, if the memory is reclaimed.

My dynamic version of the constructor (which worked fine as such) was:

Customer::Customer()
{
this->capacity = 2;
this->name = "?";
this->accounts = new ForeignAccount[capacity];
this->numAccounts = 0;
}

And the corresponding header:

private:
int capacity;
string name;
ForeignAccount* accounts;
int numAccounts;

public:
Customer();
virtual ~Customer();

How do I make a version of this that produces the set number of accounts arrays for each customers object using static memory allocation rather than dynamic?

(As for automatic vs static arrays, I have looked into my books and searched the Internet without finding something very clearifying, so I still don't understand why my current solution produces an automatic local array, or even what that really is).



http://en.wikipedia.org/wiki/Automatic_variable

In C and C++, the static keyword means the variable is not visible outside the current translation unit when used at global scope, that only one variable is available to all objects of the class when used within a class definition and, when used in a function, that the variable will only be initialized once and persist for the duration of the program (and won't be visible outside of the function.)

The only way to accomplish what you're asking for is to make accounts a fixed size and keep track of how many accounts are actually there.

1
2
3
4
5
6
7
8
 class Customer {
     //.. 

private:
     enum { accountsCapacity=256 };

     ForeignAccount accounts[accountsCapacity] ;  //
};
Last edited on
Thanks a million cire! Now it works...

I failed to realise that since the fixed size is put on the stack, the array needs to be initialised in the header already so that it gets in there at compile time.

Thanks for the other clearifications as well.

Best,

J
Topic archived. No new replies allowed.