Char* dynamic allocation??

Theoretically, after use dynamic allocation we should use "delete" as below. However, I got a error message like this:Tongue Tied

****************************************
Debug Assertion Failed!
Program : C:/hello.exe
File: dbgdel.cpp
Line: 47
Expression:_BLOCK_TYPE_IS_VALID(pHEAD->nBlockUse)
****************************************

int main(){

char* s1 = new char[30];
char* s2 = new char[30];
char com[40];

 cin.getline (com,40);
 while(strcmp(com,"exit") != 0){      
  if(strcmp(com,"help") == 0){
     sayHello();
  }//end if
  else{
   s1 = strtok (com," ");
   if(c1 == NULL)
   {
       cout<<"Please enter name"<<endl;
   }
   else{
    s2 = strtok (NULL," ");
    if(c2 == NULL){
        cout<<"Please enter address"<<endl;
    }
    else
    {............................
      .........................

   delete [] s1;
   delete [] s2;
   return 0;
}




Answer this question

Char* dynamic allocation??

  • sdfwvgf

    s1 and s2 are both reinitialized by the calls to strtok() and no longer point at the original allocated memory.

  • ta128

    Yes, Jensen is absolutely right. Also, let me give you a few "general" tips.

    1. Indent your code.

    [code language="c#"]
    int main()
    {
          cout << "Let me say numbers 1 to 10:\n" << endl;
          for(i=0; i<11; i++)
          {
               cout << "\t" << i;
          }

          cout << "\nHave a nice day!" << endl;
          return 0;
    }
    [/code
    ]

    That's an example of indentation. There is minor indentation in your code, but still not enough.

    2. Insert your code in the code area! They're meant just for that.

    3. Refer to standards, always. C++ is definately not one toy you can play around with. Have these two great books and you're set:

       a. The Complete C++ Reference -- Herbert Schildt
       b. The C++ Programming Language -- Bjarne Stroustrup

    Hope I helped. Feel free to ask more questions, this will encourage you to learn more and give you lots of confidence. Good luck!


  • Char* dynamic allocation??