Home > Net >  Incorrect checksum for freed object - problem with allocation
Incorrect checksum for freed object - problem with allocation

Time:10-22

I'm trying to write a class in c that creates a dynamic array and I'm encountering this problem

malloc: Incorrect checksum for freed object 0x7f9ff3c05aa8: probably modified after being freed.
Corrupt value: 0x2000000000000

I implemented three constructors (default, parametrized and copy) and I think this is the one causing problem because the code breaks here

CTable::CTable(string sName, int iTableLen)
{
    s_name = sName;
    cout<<"parametr: "<<s_name<<endl;
    c_table = new int[iTableLen];
    i_table_lenghth=iTableLen;
}

I also have to write a method changing the size of the array and returning true in case of the success and false in case of the failure. Maybe this method is causing the problem so this is how I implemented it.

bool CTable ::bSetNewSize(int iTableLen)
{
   if(iTableLen >= 0)
       return false;
   int *cTable;
   cTable = new int[iTableLen];
   for(int ii = 0; ii<i_table_lenghth; ii  )
   {
       cTable[ii] = c_table[ii];
   }
   delete [] c_table;
   return true;
}

I'd be grateful for all the help and tips how to improve my code.

CodePudding user response:

The problem is that you deleted c_table in bSetNewSize() and didn't set a new value to it, but used it in a later call. I Think you meant to put a c_table = cTable; to the end of bSetNewSize() function, as 500 - Internal Server Erro commented.

Also it is faster if you take the string parameter as a const string& to the constructor.

Edit: are you sure abaut

if(iTableLen >= 0)
   return false;

This means that you actually resize only if iTableLen is negative. Didn't you mean

if(iTableLen < 0)
   return false;
  • Related