difference between delete p and delete [] p in following program.

I know delete p deallocates memory pointed to by p and delete [] p deallocates array.
But what's the difference in the following program?

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;


int main(){
	int *p = new int [30];
	p[0] = 1;
	p[1] = 5;
	cout << p[0]  << endl << p[1] << endl << endl;
	delete p;//delete [] p; BOTH WORK FINE AND IN EXACTLY THE SAME WAY. WHY?
	cout << p[0]  << endl << p[1] << endl << endl;
	return 0;
}
delete p will delete only p[0]. Other 29 entries will not be deleted and this memory will leak.
delete[] p will delete whole array.

Why do you think, that they both works the sameway, and, in fact, work at all?
I suggest writing a Whine class. (Or use this one I wrote myself.)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>

struct Whine
{
    Whine()
    {
        std::clog << this << " Whine::Whine()\n";
    }

    ~Whine()
    {
        std::clog << this << " Whine::~Whine()\n";
    }

    Whine(const Whine &w)
    {
        std::clog << this << " Whine::Whine(const Whine &w), &w == " << &w << '\n';
    }

    Whine & operator = (const Whine &w)
    {
        std::clog << this << " Whine& Whine::operator=(const Whine &w), &w == " << &w << '\n';
        return *this;
    }
};


1
2
3
4
5
6
int main()
{
    Whine *wp = new Whine[5];

    delete wp;
}
0x3e2474 Whine::Whine()
0x3e2475 Whine::Whine()
0x3e2476 Whine::Whine()
0x3e2477 Whine::Whine()
0x3e2478 Whine::Whine()
0x3e2474 Whine::~Whine()


1
2
3
4
5
6
7
8
9
10
int main()
{
    Whine *wp = new Whine[5];

    delete[] wp;
}
0x3e2474 Whine::Whine()
0x3e2475 Whine::Whine()
0x3e2476 Whine::Whine()
0x3e2477 Whine::Whine()
0x3e2478 Whine::Whine()
0x3e2478 Whine::~Whine()
0x3e2477 Whine::~Whine()
0x3e2476 Whine::~Whine()
0x3e2475 Whine::~Whine()
0x3e2474 Whine::~Whine()
Last edited on
Back in the old days, if you called:
 
Thing* p = new Thing;

You needed to call:
 
delete p;


... and if you called:
 
Thing *p = new Thing[20];

you needed to call:
 
delete [20] p;


That was eventually relaxed to:
 
delete [] p;


IF you remember this, you'll never get the two forms confused.
Undefined behaviour is undefined
Also, ¿why are you accessing out of bounds?
In delete p; why p[1] is deleted?
In delete p; why p[1] is deleted?


There is no way to know what has been deleted from the bits you posted. deleted does not mean "reset to 0". A variable can be reset to 0 without being deleted, and can be deleted without being reset to 0.

So my answer to this question is: it might not be deleted. You have no way to know.

BOTH Give the same output. WHY???


Rehan: It's like ne555 said. Undefined behavior is undefined.

If you allocate with new you must clean up with delete. If you allocate with new[] you must clean up with delete[]

Only if you do that, are the results predictable and correct.

If you stray from that, you get into area that's "undefined" which means the compiler is free to do whatever it wants. It might delete all of it, it might delete only the first element, it might delete none of it. It might crash your program, or it might write a text file to the disk telling you that you're a bad programmer. Anything would be "legal" because it's undefined.

The new[] and delete[] pairing of this code snippit:
http://liveworkspace.org/code/3XHEdL$2

Is correct. Whereas the new[]/delete pairing of the other code snippit is undefined ... therefore "anything goes".


However both snippits are accessing bad memory on line 11 so you're doing undefined behavior in both of them.
Thanks a lot Disch!
Last edited on
Topic archived. No new replies allowed.