what call is this?

This is a discussion on what call is this? within the C++ Programming forums, part of the General Programming Boards category; If five and two are objects of type Account, this should not be a call to a copy constructor? Code: ...

  1. #1
    rox
    rox is offline
    Registered User
    Join Date
    Nov 2003
    Posts
    34

    what call is this?

    If five and two are objects of type Account, this should not be a call to a copy constructor?

    Code:
    	Account five(two);
    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

  2. #2
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244
    Show the code!
    Nothing more to tell about me...
    Happy day =)

  3. #3
    rox
    rox is offline
    Registered User
    Join Date
    Nov 2003
    Posts
    34
    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(); 
    };

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,645
    >> 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

  5. #5
    rox
    rox is offline
    Registered User
    Join Date
    Nov 2003
    Posts
    34
    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

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,645
    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

  7. #7
    rox
    rox is offline
    Registered User
    Join Date
    Nov 2003
    Posts
    34
    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;
    	}

  8. #8
    rox
    rox is offline
    Registered User
    Join Date
    Nov 2003
    Posts
    34
    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?

  9. #9
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,645
    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

  10. #10
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,645
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. minix system call pls help for project
    By porvas in forum Linux Programming
    Replies: 2
    Last Post: 06-14-2009, 02:40 AM
  2. Error C2664 - Trying to call an external Dll
    By jamez05 in forum C++ Programming
    Replies: 3
    Last Post: 08-08-2006, 06:07 AM
  3. Class won't call
    By Aalmaron in forum C++ Programming
    Replies: 3
    Last Post: 04-13-2006, 04:57 PM
  4. Iterative Tree Traversal using a stack
    By BigDaddyDrew in forum C++ Programming
    Replies: 7
    Last Post: 03-10-2003, 04:44 PM
  5. call by reference and a call by value
    By IceCold in forum C Programming
    Replies: 4
    Last Post: 09-08-2001, 05:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21