-
what call is this?
If five and two are objects of type Account, this should not be a call to a copy constructor?
copy constructor being declared:
Code:
Account::Account(const Account & acct){
am I wrong? it appears to be giving a segmentation fault at the call...
this does not help me much either:
Program received signal SIGSEGV, Segmentation fault.
0x420738c9 in free () from /lib/tls/libc.so.6
-
-
This is in main:
Code:
char d[] = " 113423454567987,83456,Baker,Susan,St. Louis-de-ha-ha,416-555-5599;";
char num[] = "aw0000000000000";
Account two(d, num, 5347);
Account three;
three.init(d, num, 5347);
Account four;
four = three;
Account five(two);
this is my copy constructor:
Code:
Account::Account(const Account & acct){
printf("is it calling here?");
if (customer) {
delete [] customer;
}
try {
customer = new char[strlen(acct.customer)];
}
catch( bad_alloc) { customer = NULL; }
if(customer){
strcpy(customer, acct.customer);
strcpy(accountNumber, acct.accountNumber);
balance = acct.balance;
}
}
and it's not getting to the printf...
and this is the header file:
Code:
class Account {
char *customer;
char accountNumber[16];
int balance;
public:
Account();
Account(const Account&);
Account(const char c[], const char num[], int b);
void init(const char c[], const char num[], int b);
void changeCustomer(const char c[]);
int changeCustomerInfo(char c[]);
void changeAccountNumber(const char num[]);
const Account& operator= (const Account&);
void changeBalance(int b);
void getLastName(char st[]);
void getFirstName(char st[]);
void getCity(char st[]);
void getPhoneNumber(char st[]);
const char* getCustomer() const;
const char* getAccountNumber() const;
int getBalance() const;
~Account();
};
-
>> this should not be a call to a copy constructor?
That's exactly what it is.
>> it appears to be giving a segmentation fault at the call...
>> Program received signal SIGSEGV, Segmentation fault. 0x420738c9 in free() from /lib/tls/libc.so.6
You have memory management issues.
>> customer = new char[strlen(acct.customer)];
This does not allocate space for the NULL terminator.
You probably have other problems......show your constructor, destructor and operator equals as well.
gg
-
so I'm doing
Code:
customer = new char[strlen(acct.customer) + 1];
but than when I copy with
Code:
strcpy(customer, acct.customer);
the end of string does not get copied, right? do I have to add it in there like:
Code:
customer[strlen(acct.customer)+1] = '\0';
???
Here's the rest.
Code:
Account::Account(){
customer = 0;
init(",,,;", "000000000000000", 0);
}
Account::Account(const Account & acct){
printf("is it calling here?");
if (customer) {
delete [] customer;
}
try {
customer = new char[strlen(acct.customer)];
}
catch( bad_alloc) { customer = NULL; }
if(customer){
strcpy(customer, acct.customer);
strcpy(accountNumber, acct.accountNumber);
balance = acct.balance;
}
}
Account::Account(const char c[], const char num[], int b){
cout<<"length of passed string for customer is: "<<strlen(c)<<endl;
customer = new char[strlen(c) + 1];
strcpy(customer, c);
customer[strlen(c) + 1] = '\0';
cleanSpace(customer);
strcpy(accountNumber, num);
balance = b;
}
Account::~Account() {
if(customer) {
delete [] customer;
}
}
void Account::init(const char c[], const char num[], int b){
try {
customer = new char[strlen(c) + 1];
}
catch (bad_alloc) { customer = NULL; }
if(customer){
strcpy(customer, c);
customer[strlen(c) + 1] = '\0';
cleanSpace(customer);
strcpy(accountNumber, num);
balance = b;
}
}
const Account& Account::operator= (const Account & acct){
strcpy(this->customer, acct.customer);
strcpy(this->accountNumber, acct.accountNumber);
this->balance = acct.balance;
return *this;
}
By the way, thanks for the help :)
-
Here's what you need to remember about strings:
Code:
char s[] = "A"; // strlen(s) is 1, but requires 2 bytes of storage
char s[] = {'A', 0}; // same as above
strcpy() will copy the NULL terminator for you. However, when using strcpy(), you must gaurentee that the destination string is large enough to contain the source.
In your init():
>> customer = new char[strlen(c) + 1];
This allocates the correct amount of memory. However, call init() twice and you have a memory leak.
>> strcpy(customer, c);
This is ok - we know the destination buffer is large enough to contain c.
>> customer[strlen(c) + 1] = '\0';
No. When in doubt, just think of a 1 character string:
Code:
char s[] = "A"; // s[0] and s[1] are the only valid accesses
s[strlen(s)+1] = 0; // so is this a valid access?
strcpy() will copy the NULL terminator for you anyhow.
>> cleanSpace(customer);
Comment this out untill you have your construction, destruction, and copy'n working correctly.
>> strcpy(accountNumber, num);
Dangerous - what happens when some passes in a string with more than 15 characters?
In your constructor with parameters:
Just remove that code and call init(). If you ever see two parts of you code doing the exact same thing - consider consolidating that code into a singe function/method.
In your copy constructor:
See above comments.
In your operator equals:
See above comments.
Also realize that you're incorrectly assuming that this->customer is large enough to hold acct.customer. Just let init() take care of it.
gg
-
ok, so I make my init like so:
Code:
void Account::init(const char c[], const char num[], int b){
cout<<"in init next is check for customer"<<endl;
if(customer) {
delete [] customer;
}
cout<<"if customer existed is deleted"<<endl;
try {
customer = new char[strlen(c) + 1];
}
catch (bad_alloc) { customer = NULL; }
cout<<"new customer is created"<<endl;
if(customer){
cout<<"customer to be copied"<<endl;
strcpy(customer, c);
cout<<"cleanSpace on customer"<<endl;
cleanSpace(customer);
cout<<"accountNumber to be copied"<<endl;
strcpy(accountNumber, num);
cout<<"balance to be copied"<<endl;
balance = b;
}
cout<<"end of init"<<endl;
}
and it gives segmentation fault at:
Code:
if(customer) {
delete [] customer;
}
-
god damn it... it compiles, works and passed all the test on AIX but it gives a segmentation fault on my personal linux box...
is there something wrong in my program or it's just the compiler not liking something?
-
If you're going to post code, post all of it and I'll tell why you're getting seg. faults.
>> is there something wrong in my program
You bet ya :)
gg
-
In your init():
>> if(customer){
This assumes that customer has bee initialized to something - which is only being done in one of your 3 constructors.
That will take care of you construction, destruction, and copy'n - time to start testing the rest of it. You should also be working on your gdb skillz to help track these issues down.
gg