×

C++ Tutorial

C++ Data types

C++ Operators & Keywords

C++ Conditional Statements

C++ Functions

C++ 'this' Pointer, References

C++ Class & Objects

C++ Constructors & Destructors

C++ Operator overloading

C++ 11 (Advance C++)

C++ Preparation

C++ Header Files & Functionsr

Data Structure with C++

C++ - Miscellaneous

C++ Programs

Memory leaks/holes in C++ programming language

What is Memory Leaks/Memory Holes in C++ programming language, how can we manage the memory so memory leaks, holes can be prevented?

In Dynamic Memory Allocation, if any pointer losses (i.e. pointer does not point any memory location), program cannot access that particular memory location through the pointer.

This type of memory neither be accessed nor be allocated to other variables. So, we consider such type of memory as a lost memory.

This type of situation known as "Memory Leaks/ Memory Holes", you should worry about such type of memories and write a good programming code to manage them. So, keep in mind whenever you allocate the memory at run time, it must be released.

Consider this code

int main()
{
	int *p; //pointer declaration
	int i=0;

	//allocating space for 5 integers
	p = new int[5];

	cout<<"Enter elements :\n";
	for(i=0;i<5;i++)
		cin>>p[i];

	cout<<"Input elements are :\n";
	for(i=0;i<5;i++)
		cout<<p[i]<<endl;
	
	return 0;
}

In this code, memory for 5 integers is declaring at run time and we didn’t use delete to free the occupied memory at run time.

This may cause of "Memory Leaks or Memory Holes"

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.