CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Apr 2002
    Posts
    65

    delete [] problems

    My application is crashing when I try and delete an array I created.
    I can't figure out why? Can anyone help?? See lines 5 and 13 below.
    Also at line 4 should I initialize my pointer to null?


    1 bool CNameDialog::OnPerformSelect(void)
    2 {
    3 bool Result=true;
    4 int *iArrayOfIndex;
    5 iArrayOfIndex = new int[iCount];
    6 Result=CheckName();
    7 if(Result)
    8 {
    9 // Use iArrayOfIndex as an array
    10
    11 }
    12
    13 delete [] iArrayOfIndex;
    14 return Result;
    15}

  2. #2
    Join Date
    May 2000
    Location
    Washington DC, USA
    Posts
    715
    Where does iCount get set?

    Your problem isn't in this code that I can see. Your problem is almost certainly that you are walking on memory. Make sure that you're not writeing past the allocated memory buffer.

    One thing I would try is to take away the square brackets on the delete statement. VC++ 5 required you to do that but I don't believe 6 or .net does. Could be wrong but it's worth a shot.

    Like I said your issue is that your walking on memory. The way to find this issue is to memset your entire array to '/0' and then to watch the last byte as you step through your code. See where it gets written.

  3. #3
    Join Date
    Jun 2002
    Posts
    224
    I hope this helps:
    Code:
    int main()
    {
      const int c = 2;
      int* p = new int[c];
      for ( int i = 0; i <= c; i++ ) // trouble: <= instead of <
        p[i] = i;
      delete[] p; // crash: origin: <=
      return 0;
    }
    ZDF

    What is good is twice as good if it's simple.
    "Make it simple" is a complex task.

  4. #4
    Join Date
    May 2000
    Location
    Phoenix, AZ [USA]
    Posts
    1,347
    Despite what one of the other posters above said, don't remove the square brackets on the delete statement. When you're allocating an array, you must call delete [] and not delete. I can never remember the exact reason and I'm sure I'll be corrected, but I seem to remember that if you leave off the [], the destructor for ONLY the first item in the array will get called.

    --Paul

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449
    Originally posted by PaulWendt
    Despite what one of the other posters above said, don't remove the square brackets on the delete statement. When you're allocating an array, you must call delete [] and not delete. I can never remember the exact reason and I'm sure I'll be corrected, but I seem to remember that if you leave off the [], the destructor for ONLY the first item in the array will get called.

    --Paul
    The only reason is that using delete on something allocated with operator new [] (and vice-versa) leads to undefined behavior. What the behavior is, is up to the compiler. It could do a whole host of things from working flawlessly, to crashing your program. Therefore you never mix them up:

    new --> delete
    new[] --> delete[]

    Regards,

    Paul McKenzie

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449
    Originally posted by JMS
    One thing I would try is to take away the square brackets on the delete statement. VC++ 5 required you to do that but I don't believe 6 or .net does. Could be wrong but it's worth a shot.
    No, never do this. It is not a matter of a compiler requiring it or not. If you use new[] to allocate memory, you always use delete[], regardless of what may or may not work, or what compiler you're using. That is the rule of C++ dynamic memory management. Anything else, and you are invoking undefined behavior.

    Regards,

    Paul McKenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured