Heap Corruption error in C++

Hello,
In destructor when I w
ant delete array I have heap corruption error.
If I add array = NULL; before delete[] array, this problem has been solved.
could you please more explain it?
Why I should add array = NULL; in Line 16?


Thanks in advance
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
26
27
28
29
30
31
32
33
34
#include<iostream>
using namespace std;

class K
{
private:
	int length;
	int* array = new int[length];
public:
	K(int length1)
	{
		length = length1;
	}
	~K()
	{
		
		delete[] array;
	}
	void Add(int n)
	{
		array[0] = n;
	}
};

	int main()
	{
		int a;
		cin >> a;
		K obj(a);
		obj.Add(10);
		obj.~K();

	}
Last edited on
Don't call the destructor manually. It is called when the lifetime of the object ends.

Don't assign NULL *before* calling delete. You just created a memory leak because you re-assigned the pointer to point to no memory.

Calling delete on a nullptr is a no-op.
1
2
3
int* a = new int;
a = nullptr; // MEMORY LEAK
delete a; // no-op... still have memory you didn't delete 

(This applies to both delete and delete[])
Last edited on
@Ganado
Thank you for your answer, so when we should call the destructor manually?
Last edited on
I also edited my post with more information.

so when we should call destructor manually?
Only in really rare situations.
See: https://stackoverflow.com/questions/14187006/is-calling-destructor-manually-always-a-sign-of-bad-design
I personally have never had to do it, but there are people more experienced than me that have.
Last edited on
Line 31: Do not call the destructor directly. It is called automatically when main() ends.

The constructor is also wrong. Change to:
1
2
3
4
5
6
7
	int* array = nullptr; // new int[length]; --> length is invalid at that time
public:
	K(int length1)
	{
		length = length1;
		array = new int[length];
	}
1
2
3
int* a = new int;
a = nullptr; // MEMORY LEAK
delete a; // no-op... still have memory you didn't delete  


So what does the above code doing? (What does removes it?)
The problem is in your constructor. When the object is constructed, the data members are constructed, and then the body of the constructor runs.
- There's nothing to construct length, so it gets whatever random data is in memory at that moment.
- The array member is constructed using the the uninitialized length member.
- The body of the constructor runs and you set length to length1.

I think you can fix this by changing the constructor to:
K(int length1) : length)length1) {}
This will construct length() from length1. Then array will get assigned from length.
Thanks coder777 I was focusing on the destructor and not the constructor :P

@Shervan360
So what does the above code doing?

* Line 1 allocates an int with a dynamic lifetime ("on the heap").
* Line 2 then makes the pointer to that dynamic memory now point to nothing (nullptr). The original memory still exists and has not been cleaned up. This is now a memory leak, because you have no way of accessing or deleting the memory you created on Line 1.
* Line 3 is calling delete on a nullptr (same thing as NULL), and is a no-op.
In other languages like C# and Java, we have Garbage Collector.
In C and C++ we don't have garbage collector and is better we don't use destructor manually. Are our programs in C++ are optimal(has good performance)?
Because of all memory allocation in C++ without a garbage collector deleted after the end of the program.
Last edited on
Topic archived. No new replies allowed.