CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Aug 2003
    Posts
    1
    In regard to the following FAQ...

    This answers the question WHEN to use the different delete operators, not the difference between them.
    Does anyone have a more in-depth answer to the original question?


    [Andreas]: Added link to the FAQ...
    Last edited by Andreas Masur; August 26th, 2003 at 03:34 PM.

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449
    Originally posted by Hopplos
    In regard to the following FAQ...

    This answers the question WHEN to use the different delete operators, not the difference between them.
    Does anyone have a more in-depth answer to the original question?


    [Andreas]: Added link to the FAQ...
    The difference is simple -- the compiler's implementation of operator delete and operator delete[] may be different, and not calling the correct one may introduce undefined behavior or memory leaks.

    Also operator delete and operator delete[] can be overloaded. The programmer who has overloaded these operators may have written totally different implementations of delete and delete[]. The programmer who calls the wrong version, again, may introduce bugs by using the wrong form of delete.

    For example, consider the following code:
    Code:
    class A
    {
    };
    
    class foo
    {
        public:
             A *p;
             foo() { p = new A; }
             ~foo (delete p;}
    };
    
    int main()
    {
        foo *pFoo = new foo[50];
        delete pFoo; // incorrect
    }
    For most implementations, the incorrect call "delete pFoo" will only delete foo[0]. The other foo's are left undestroyed since you didn't indicate that an array of foo's are to be destroyed.

    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