Hey Guys,

Im having a spot of trouble with dynamic memory allocation and any help would be appreciated.

My problem is the I have 2 classes Bank and Account and I am making a dynamic array of the Account member variables. Everything seems to work fine until I try to dynamically allocate the array and the program crashes.

//acc.h
class Account{
private:
	//Declaring class member variable
	char *customer;				//251 including null byte
	char accountNumber[20 + 1];		//16 including null byte
	int balance;
....
};
//bank.h
class Bank{
private:
	Account *savings;
       int arraySize;
...
Bank{name[]);
};

///In the bank class constructor.
		savings = new Account[arraySize];

I know my syntax is correct I just cant figure out whats wrong.
PS. THis is not the whole code as its about a 1000 lines so if you want I can PM it.

Thanks in advance.

Please post the entire class definition for Account and for Bank.

From class Account { to }; and the same for Bank. (For this part, I'm not really interested in member bodies, I just want a feel for what data items the class contains and the methods you have defined.)

Then I would like to see the contents of the constructors for Account and Bank.

The definitions for Account Class

class Account{
private:
	char *customer;				
	char accountNumber[20 + 1];		
	int balance;

public:
	Account();								
	Account(const Account&);				
	Account(const char c[], const char num[], int b);	
	~Account();

	friend class Bank;
};

Constructors for Account Class

Account::Account(){
	customer = NULL;
	strcpy(customer, ",,,;");		
	strcpy(accountNumber, "000000000000000");		
	balance = 0;
}

Account::Account(const Account& copy){
	customer = copy.customer;
	strcpy(accountNumber, copy.accountNumber);
	balance = copy.balance;
}

Definition for the Bank Class

class Bank{
private:
	Account *savings;
	FILE* fp;
	int size;
	int arraySize;

public:
	Bank();
	Bank(char []);
	~Bank();
};

Definitions for the Bank Class Constructors

Bank::Bank(){
	fp = 0;
	savings = NULL;
	size = 0;
	arraySize = 0;
}

Bank::Bank(char file[]){
	int n;
	fp = 0;
	arraySize = 0;
	size = 0;
	int bal;
	char anum[15 + 1];
	char cust[315 + 1];
	savings = NULL;

	//open file 
	fp = fopen(file, "r");

	//check to see if file opened 
	if(fp == NULL){
		cout << "The file could not be found or opened." << endl;
	}
	else{
		fscanf(fp, "%d:", &n);
		arraySize = n + 10;

		//Having a problem HERE
		savings = new Account[arraySize];

	int i = 0;
		//loop until end of file and copy values into array
		while(fscanf(fp, "%15[^,],%d,%315[^;];", anum, &bal, cust) != EOF){
			strcpy(savings[i].accountNumber, anum);
			savings[i].balance = bal;
			strcpy(savings[i].customer, cust);
			i++;
			size++;
			cout << i << "::" << anum << "::" << bal <<  "::" << cust << endl;
		}
	}
}

The default constructor for Account:

Account::Account(){
	customer = NULL;
	strcpy(customer, ",,,;");		
	strcpy(accountNumber, "000000000000000");		
	balance = 0;
}

Is what gets called when you call new Account[arraySize] On line 2 of the code above, you set customer to point to NULL.
Then on line 3, you attempt to strcpy into the NULL

Very bad form. You need to either declare customer like a bigger accountNumber, or you need to allocate the space you want with something like customer = new char[251]; and then make sure to delete it in the destructor. (The only problem is that the copy constructor just makes a copy of the pointer. If you decide to allocate the customer, the copy constructor will too.)

PS - if you define a copy constructor, you almost always want to define an assignment operator (operator =) to make sure the handling is the same in both cases.

Fix that and see if your problem goes away.

Thank you. I commented Line 2 as you said and I managed to get my program to work some what, I have a few more errors do I'll work them out before I mark this thread solved.

> char *customer; //251 including null byte
> char accountNumber[20 + 1]; //16 including null byte
This is C++, use std::string and move on?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.