| | |
Error with Dynamic Memory Allocation
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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.
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.
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.
C++ Syntax (Toggle Plain Text)
//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; .... };
C++ Syntax (Toggle Plain Text)
//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.
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
And the crowds screamed in terror and cowered in fear for Microsoft had approached.
From the testament of 10011101
•
•
Join Date: May 2008
Posts: 538
Reputation:
Solved Threads: 86
Please post the entire class definition for Account and for Bank.
From
Then I would like to see the contents of the constructors for Account and 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
Constructors for Account Class
Definition for the Bank Class
Definitions for the Bank Class Constructors
C++ Syntax (Toggle Plain Text)
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
C++ Syntax (Toggle Plain Text)
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
C++ Syntax (Toggle Plain Text)
class Bank{ private: Account *savings; FILE* fp; int size; int arraySize; public: Bank(); Bank(char []); ~Bank(); };
Definitions for the Bank Class Constructors
C++ Syntax (Toggle Plain Text)
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; } } }
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
And the crowds screamed in terror and cowered in fear for Microsoft had approached.
From the testament of 10011101
•
•
Join Date: May 2008
Posts: 538
Reputation:
Solved Threads: 86
The default constructor for Account:
Is what gets called when you call
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
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.
c++ Syntax (Toggle Plain Text)
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.
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
And the crowds screamed in terror and cowered in fear for Microsoft had approached.
From the testament of 10011101
![]() |
Similar Threads
- Dynamic memory allocation with strings (C)
- 2D array memory allocation error (C++)
- Error:Null Pointer assignment (C++)
- Please Help Dynamic Memory Allocation. (C++)
- dynamic memory allocation (C++)
- GAAAH! Memory issue? (C++)
- getting null pointer assignment error (C)
- Dynamic Memory Allocation - offset pointers (C)
- Dynamic memory allocation homework (C++)
Other Threads in the C++ Forum
- Previous Thread: cannot figure out storing character into character array
- Next Thread: doubt in mfc....very urgent!!!!!!!
| Thread Tools | Search this Thread |
api array beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion count data database delete desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game getline google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text text-file tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






