0
votes

This is the error i am getting:

======= Memory map: ========
08048000-0804a000 r-xp 00000000 00:28 2955225621  /home/w5/w5
...more
Aborted

My program compiles perfect, also part of my output is correct. I have 3 files, w5.cpp, employee.h, employee.cpp. I need to create a copy constructor, an assignment operator, and a destructor.

w5.cpp (its a little big). http://hostcode.sourceforge.net/view/1435.txt

employee.h class Employee {

   int empNum;
   char* empName;

   public:
     //Constructor 
      Employee();
      Employee(int num, const char* name);

     //Copy Constructor
      Employee(const  Employee& source);

     //Assignment operator
      Employee& operator=(const Employee& source);

     //Destructor
      ~Employee();

      void display() const;
      bool isGreaterThan(const Employee&) const;

};

employee.cpp

#include <iostream>
using namespace std;
#include "Employee.h"
#include <string.h>

Employee::Employee() {
   empNum = 0;
   empName= nullptr;
}
Employee::Employee(int num, const char* name) {

   if(num < 0 || strcmp(name,"")==0) {
      Employee();
   } 
   else {
      empNum = num;
      empName = new char[strlen(name)+1];
      strcpy(empName, name);
   }
}

Employee::Employee(const Employee& source) {

   cout << "Copy Constructor!"<<endl;

   if(source.empName !=nullptr) {
      empNum = source.empNum;
      empName = new char[strlen(source.empName)+1];
      //strcpy(empName, source.empName);
      empName = source.empName;
   }
   else 
      empName =nullptr;
}

Employee& Employee::operator=(const Employee& source) {

   cout << "Operator Assignment!" <<endl;          

   // check for self-assignment
   if(this != &source) {
       cout << "Operator Assignment 2" <<endl;

      empNum = source.empNum;
      delete [] empName;
      if(source.empName !=nullptr) { 
         empName = source.empName;
         //strcpy(empName, source.empName);
      }
      else
         empName = nullptr;
   }
   return *this;
}

//Destructor
Employee::~Employee() {
   delete [] empName;                     
}

void Employee::display() const {

   cout << empNum << empName <<endl;
}

bool Employee::isGreaterThan(const Employee& source) const {

   return true;
   //still need to code here
}

To compile this I use g++ -std=c++0x -o w5 w5.cpp Employee.cpp After looking online regarding this problem I think I have a "non valid pointer" but i'm not sure where.

3

3 Answers

1
votes

One problem I see is in your assignment operator:

// these two lines should be removed
empNum = source.empNum;
delete [] empName; // this deletes the source's employee name! (bad)

if (source.empName != nullptr) { 
     empName = source.empName;
}

You shouldn't just assign your object's empName pointer to the memory allocated by source since source will delete this memory when the destructor is called. You should allocate new memory for the string and then copy into it:

// first delete our string if we've previously allocated memory for it
if (empName != nullptr) {
    delete [] empName;
    empName = nullptr;
}

// now copy the source's empName
if (source.empName != nullptr) { 
     empName = new char[strlen(source.empName)+1];
     strcpy(empName, source.empName);
}
1
votes

Change it back to strcpy:

empName = source.empName;

Otherwise you are double freeing this pointer

Also your copy constructor correctly allocates memory, while operator= does not.

0
votes

In the copy constructor we are supposed to create copy of all the pointers .

Employee::Employee(const Employee& source) {

   cout << "Copy Constructor!"<<endl;

   if(source.empName !=nullptr) {
      empNum = source.empNum;
      empName = new char[strlen(source.empName)+1];
      //strcpy(empName, source.empName);
      empName = source.empName;
   }
   else 
      empName =nullptr;
} 

the line "empName = source.empName;" will result in empName of the object as well as the empName of the src object pointing to the same location . Also the memory is leaked too .

in assignment opertor too you have the same problem . dont assign pointers do a strcpy or memcpy after you have allocated memory . also check for the pointer value before freeing it .