Hi every one :)
Well I wrote this code for deleting the pointers, and it compiles and runs. But at the end of the run it comes up with an Debug Assertion failed.

WHY???

#include <iostream>

using namespace std;

int main()
{
	bool boolVar = true;
	int intVar = 50;
	float floatVar = 3.14f;

	bool* boolPtr = &boolVar;
	int* intPtr = &intVar;
	float* floatPtr = &floatVar;

	cout << "boolVar  = " << boolVar << endl;
	cout << "intVar  = " << intVar << endl;
	cout << "floatVar  = " << floatVar << endl;

	cout << endl;

	cout << "boolPtr  =  Address of boolVar = " << boolPtr << endl;
	cout << "intPtr  =  Address of intVar = " << intPtr << endl;
	cout << "floatPtr  =  Address of floatVar = " << floatPtr << endl;

	delete boolPtr;
	boolPtr = 0;
	delete intPtr;
	intPtr = 0;
	delete floatPtr;
	floatPtr = 0;

}

Hi invisi,

The problem is obvious. Do remember this rule. Use delete when you have use a "new". In your case, you did not "new" a variable. You are merely assigning the pointer to another variable. As a matter of fact, this cannot be deleted. If you are using the "new" keyword to allocate and memory location of storing your variable, only then you'll have to use delete.

Again, in your case, you may want to assign the pointer to null (which mean reference to nothing)

Hope this helps...


Regards,
Nick

> But at the end of the run it comes up with an Debug Assertion failed.
> WHY???

In your case, you're deleting what you didn't new.

If you don't call new for a pointer, don't call delete.

Examples.

int *p = new int ; .... delete p;
int *q = new int[10]; ... delete [] q;

If you used [] in the new, then you MUST use [] in the delete as well.

Other causes of trouble
- running off the end of the allocated memory (say by using <= in a loop rather than < ) MAY cause an assertion, or a segfault
- trying to delete the same pointer twice MAY cause an assertion, or a segfault
- doing some pointer calculations, and then deleting a pointer which is no longer at the start of the memory allocated MAY cause an assertion, or a segfault

Note that just because you do something wrong, there is NO guarantee of a run-time diagnostic. The surprise can be deferred until much later on.

Hi Narue,

I tried debugging the codes and found that p actually points to the array 'a'. I tried successfully accessing the last element of array 'a' using pointer p. Therefore, I conclude that p is not an array of size 5.

However, I do not understand what does that code int *p = new(a) int[5]; mean. Does it now allocate a dynamic memory in the heap? Why is it pointing to the array 'a' instead? Any expert here could explain?


Regards,
Nick

>^^^does that mean the array size is 100 and the first element is equal to 0?
An array of size 100 and all elements are initialized to 0. If you privide an initializer list and there aren't enough values, all remaining elements are initialized to the default value T() where T is the type of the array.

>If so, does that mean you array will look like this a[0, 100, 200,..., 99,000]?
Try it and see.

>^^^Does this mean p points to array a and the array's size equals 5?
Yes.

>why isn't the output 0 100 200 300 400 instead of 0 10 20 30 40?
It is. The comment and code don't match.

Hi invisi,

The problem is obvious. Do remember this rule. Use delete when you have use a "new". In your case, you did not "new" a variable. You are merely assigning the pointer to another variable. As a matter of fact, this cannot be deleted. If you are using the "new" keyword to allocate and memory location of storing your variable, only then you'll have to use delete.

Again, in your case, you may want to assign the pointer to null (which mean reference to nothing)

Hope this helps...


Regards,
Nick

Thanks dude :)

On the other hand, if you use new to allocate memory for the array
then you should use delete [] array, syntax. And set the other pointers
associated with the array to null.

Also Why are you doing this :

double *pd = new (a + 5) double;

when a is an int array.

Your trying to destroy your compiler aren't you?
You should not mix up data like that.

This question has already been answered. Start a new discussion instead.