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.
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.
Bookmarks