Showing posts with label delete[]. Show all posts
Showing posts with label delete[]. Show all posts
Tuesday, May 18, 2010
delete operator C++
Anything wrong with this code?
T *p = new T[10];
delete p;
Everything is correct, Only the first element of the array will be deleted”, The entire array will be deleted, but only the first element destructor will be called.
Anything wrong with this code?
T *p = 0;
delete p;
Yes, the program will crash in an attempt to delete a null pointer.
Wednesday, January 6, 2010
operator new[] and delete[] C/C++
// operator delete[] example
#include < iostream >
#include < new >
using namespace std;
struct myclass {
myclass() {cout <<"myclass constructed\n";}
~myclass() {cout <<"myclass destroyed\n";}
};
int main () {
myclass * pt;
pt = new myclass[3];
delete[] pt;
return 0;
}
Output:
myclass constructed
myclass constructed
myclass constructed
myclass destroyed
myclass destroyed
myclass destroyed
#include < iostream >
#include < new >
using namespace std;
struct myclass {
myclass() {cout <<"myclass constructed\n";}
~myclass() {cout <<"myclass destroyed\n";}
};
int main () {
myclass * pt;
pt = new myclass[3];
delete[] pt;
return 0;
}
Output:
myclass constructed
myclass constructed
myclass constructed
myclass destroyed
myclass destroyed
myclass destroyed
Subscribe to:
Posts (Atom)
