Error with Dynamic Memory Allocation

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: May 2008
Posts: 124
Reputation: kenji is an unknown quantity at this point 
Solved Threads: 8
kenji's Avatar
kenji kenji is offline Offline
Junior Poster

Error with Dynamic Memory Allocation

 
0
  #1
Jan 19th, 2009
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.

  1. //acc.h
  2. class Account{
  3. private:
  4. //Declaring class member variable
  5. char *customer; //251 including null byte
  6. char accountNumber[20 + 1]; //16 including null byte
  7. int balance;
  8. ....
  9. };
  1. //bank.h
  2. class Bank{
  3. private:
  4. Account *savings;
  5. int arraySize;
  6. ...
  7. Bank{name[]);
  8. };
  9.  
  10. ///In the bank class constructor.
  11. 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.
Last edited by kenji; Jan 19th, 2009 at 1:08 am.
And she said "Let there be light" and on the seveth day Windows booted.
And the crowds screamed in terror and cowered in fear for Microsoft had approached.
From the testament of 10011101
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 538
Reputation: Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough 
Solved Threads: 86
Murtan Murtan is offline Offline
Posting Pro

Re: Error with Dynamic Memory Allocation

 
0
  #2
Jan 19th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 124
Reputation: kenji is an unknown quantity at this point 
Solved Threads: 8
kenji's Avatar
kenji kenji is offline Offline
Junior Poster

Re: Error with Dynamic Memory Allocation

 
0
  #3
Jan 19th, 2009
The definitions for Account Class
  1. class Account{
  2. private:
  3. char *customer;
  4. char accountNumber[20 + 1];
  5. int balance;
  6.  
  7. public:
  8. Account();
  9. Account(const Account&);
  10. Account(const char c[], const char num[], int b);
  11. ~Account();
  12.  
  13. friend class Bank;
  14. };

Constructors for Account Class
  1. Account::Account(){
  2. customer = NULL;
  3. strcpy(customer, ",,,;");
  4. strcpy(accountNumber, "000000000000000");
  5. balance = 0;
  6. }
  7.  
  8. Account::Account(const Account& copy){
  9. customer = copy.customer;
  10. strcpy(accountNumber, copy.accountNumber);
  11. balance = copy.balance;
  12. }

Definition for the Bank Class
  1. class Bank{
  2. private:
  3. Account *savings;
  4. FILE* fp;
  5. int size;
  6. int arraySize;
  7.  
  8. public:
  9. Bank();
  10. Bank(char []);
  11. ~Bank();
  12. };

Definitions for the Bank Class Constructors
  1. Bank::Bank(){
  2. fp = 0;
  3. savings = NULL;
  4. size = 0;
  5. arraySize = 0;
  6. }
  7.  
  8. Bank::Bank(char file[]){
  9. int n;
  10. fp = 0;
  11. arraySize = 0;
  12. size = 0;
  13. int bal;
  14. char anum[15 + 1];
  15. char cust[315 + 1];
  16. savings = NULL;
  17.  
  18. //open file
  19. fp = fopen(file, "r");
  20.  
  21. //check to see if file opened
  22. if(fp == NULL){
  23. cout << "The file could not be found or opened." << endl;
  24. }
  25. else{
  26. fscanf(fp, "%d:", &n);
  27. arraySize = n + 10;
  28.  
  29. //Having a problem HERE
  30. savings = new Account[arraySize];
  31.  
  32. int i = 0;
  33. //loop until end of file and copy values into array
  34. while(fscanf(fp, "%15[^,],%d,%315[^;];", anum, &bal, cust) != EOF){
  35. strcpy(savings[i].accountNumber, anum);
  36. savings[i].balance = bal;
  37. strcpy(savings[i].customer, cust);
  38. i++;
  39. size++;
  40. cout << i << "::" << anum << "::" << bal << "::" << cust << endl;
  41. }
  42. }
  43. }
Last edited by kenji; Jan 19th, 2009 at 1:40 am.
And she said "Let there be light" and on the seveth day Windows booted.
And the crowds screamed in terror and cowered in fear for Microsoft had approached.
From the testament of 10011101
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 538
Reputation: Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough 
Solved Threads: 86
Murtan Murtan is offline Offline
Posting Pro

Re: Error with Dynamic Memory Allocation

 
0
  #4
Jan 19th, 2009
The default constructor for Account:
  1. Account::Account(){
  2. customer = NULL;
  3. strcpy(customer, ",,,;");
  4. strcpy(accountNumber, "000000000000000");
  5. balance = 0;
  6. }

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.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 124
Reputation: kenji is an unknown quantity at this point 
Solved Threads: 8
kenji's Avatar
kenji kenji is offline Offline
Junior Poster

Re: Error with Dynamic Memory Allocation

 
0
  #5
Jan 19th, 2009
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.
And she said "Let there be light" and on the seveth day Windows booted.
And the crowds screamed in terror and cowered in fear for Microsoft had approached.
From the testament of 10011101
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Error with Dynamic Memory Allocation

 
0
  #6
Jan 19th, 2009
> char *customer; //251 including null byte
> char accountNumber[20 + 1]; //16 including null byte
This is C++, use std::string and move on?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC