run time errors!

Why am I getting these errers at run time
//////////////////////////
Debug Assertion Failed!
File: dbgdel.cpp
.
.
Expressiong: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)
//////////////////////////
Debug Assertion Failed!
File: dbgheap.c
.
.
Expressiong: _CrtIsValidHeapPointer(pUserData)
/////////////////////////////
Debug Assertion Failed!
File: dbgheap.c
.
.
Expressiong: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)

Is there something wrong with this code (I compiled the same code using GCC for win32 with no run time error).

class cString{
      char *cData;
      unsigned int cSize;
   public:
      cString(int len);
      ~cString();
      .
      .
      .
      friend cString operator +(const cString& cS1, const cString& cS2);
};
cString::~cString(){
      delete[] cData;
}
cString::cString(int len){
      cData = new char[len+1];
      cSize = len;
}
cString operator +(const cString& cS1, const cString& cS2){ //something wrong here
      cString tmp(cS1.cSize + cS2.cSize + 1);
      tmp.cSize = cS1.cSize + cS2.cSize;
      strcpy(tmp.cData, cS1.cData);
      strcat(tmp.cData, cS2.cData);
      return tmp;
}
int main(){
     cString c1(5), c2(5);
     .
     .
     c1 + c2;       // this is where I got the errors message above trying to debug it
}

Thank You!



Answer this question

run time errors!

  • HumbleServant

    Thanks Nathansl2003,

    I think you're right about tmp object being destroyed after the function call.  I think that's why I got those messages (not sure though).  I'm surprised that GCC compiler create EXE that ran fine.  Here's what I modified to return a ptr to cString instead:
    cString *operator +(const cString& cS1, const cString& cS2){
          cString *tmp = new cString(cS1.cSize + cS2.cSize);
          tmp->cSize = cS1.cSize + cS2.cSize;
          strcpy(tmp->cData, cS1.cData);
          strcat(tmp->cData, cS2.cData);
          return tmp;
    }

    In main:

    cString c0(4), c1(5), *c2=c0+c1;

    I know there's a string class lib. in C++, but I just wanted to refresh my memory with C++.  I haven't touched it for a few years now.

  • Dirk Myers - MSFT

    I see a problem in you constructor. You allocate room, but you don't initialize the memory (char*) to have a defined value.

    so cString c1(5); creates a string point with a pointer to unitialized data. If you use the cData pointer in it, it may point to a string tat is not terminated by a null character. This might cause subsequent perations to overwrite buffer space, that causes the error.



  • dave_mwi

    Hi Moonwalker,

    I am no expert on C++, but from looking at your code you have a char pointer cData so you are using new to allocate memory for your char.  You are most likely getting those errors in your code because when you create dynamic memory with char's you need to also have the a copy constructor and assignment operator as part of your class.  If you do not have these the compiler will generate a default copy constructor and assignment operator for you.  When it does this it will do a member-by-member copy (shallow copy).  Instead of a deep copy. 

    Also in your operator + function you are returning a temporary cString object declared inside the function.  I would have to test it out but I believe that once you leave the function the tmp object is destoryed.

    Note:  you might have done the above and just left it out.

    It appears that you have left some code out of your post and you may have done all this so it is hard to   If you like you can send me your code to nathansl2003@yahoo.com and I will look at it.

    Thanks.


  • Nidonocu

    Ok, hope everything works fine now.  If you have any other questions e-mail me at the my address in the first post I made.
  • run time errors!