0

Im getting the following error while running a c++ program with xcode. The program works fine except for the custom destructor to release the dynamically allocated memory in the object "workforce." The error does not occur when running the same code with Dev c++. I prefer xcode as an IDE. Maybe there is a compiler setting that needs to be tweaked?

A4Q1(94887) malloc: * error for object 0x100100a38: pointer being freed was not allocated

workforce::workforce(int n)   // set size = n and allocate an array of employees
{
    size = n;
    list = new employee [n]; //allocate sufficient memory for n employees
}
// big 3 prototypes here
workforce::~workforce(void)
{
    delete [] list;
    //cout << "Destroying workforce"<<endl;
}
Bryan Chen
  • 45,816
  • 18
  • 112
  • 143
aaronium112
  • 2,992
  • 4
  • 35
  • 50
  • 1
    It's more likely that you are overwriting the memory that you receive while using the `list`. Dev c++ - generated code happens to be more "lenient" on it. Try running your code with [valgrind](http://valgrind.org/) to see what's going on. – Sergey Kalinichenko Oct 08 '13 at 02:16
  • You're using a naked array for seemingly no good reason. Don't. Use `std::vector`. Then you don't have to worry about deleting it. Simply make a `std::vector list` member (and not a pointer). – Kuba hasn't forgotten Monica Oct 08 '13 at 02:18
  • Don't blame the compiler, you have an error in your code. I would geuss that you've failed to follow the rule of three, http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three. But from the information provided that is only a guess. – john Oct 08 '13 at 04:53

0 Answers0