← Back to C++ Course | Chapter 14: Memory Management | Lesson 2 of 6

C++ में new और delete

new और delete आपको program चलते हुए memory माँगने और खत्म होने पर इसे वापस देने देते हैं, एक restaurant में एक table उधार लेने और खत्म होने पर leave करने की तरह। आपको इसे return करना याद रखना ज़रूरी है।
Syntax
cpp
data_type *pointer_name = new data_type(value);
delete pointer_name;

data_type *array_name = new data_type[size];
delete[] array_name;

Dynamic Memory Allocation

new operator runtime पर heap से memory का एक block request करता है और इसकी ओर एक pointer return करता है, आपको ऐसे objects बनाने देते हुए जिनका size या lifetime program शुरू होने पर पता न हो।

हर successful new को eventually एक matching delete के साथ pair होना चाहिए, नहीं तो वह memory program के बाकी run के लिए reserved रहती है -- एक memory leak।

उदाहरण: Dynamic Memory Allocation

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Allocate an object on the heap with new
	int *ptr = new int(5);
	// Print to the console with cout
	std::cout << *ptr << std::endl;
	// Free the heap memory with delete
	delete ptr;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Dynamic Arrays

delete[] वह memory release करता है जो new[] से allocate हुई थी, और arrays के लिए plain delete की बजाय इस्तेमाल होना चाहिए -- अकेला delete सिर्फ एक single object destroy करना जानता है, पहले array के हर element को walk through करके destruct करना नहीं।

new[] को delete के साथ mismatch करना (या इसका उल्टा) undefined behavior है।

उदाहरण: Dynamic Arrays

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Allocate an object on the heap with new
	int *arr = new int[3]{1, 2, 3};
	// Print to the console with cout
	std::cout << arr[1] << std::endl;
	// Free the heap memory with delete
	delete[] arr;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Dynamic Object Creation

new ClassName(args) एक object के लिए space allocate करता है और तुरंत इसका constructor call करता है, इसलिए pointer return होते ही object पूरी तरह initialized होता है। उस pointer पर delete पहले destructor चलाता है, फिर underlying memory free करता है -- यही तरीका है जिससे heap objects के लिए भी RAII-style cleanup मिलती है।

उदाहरण: Dynamic Object Creation

cpp
// Include std::cout and std::cin
#include <iostream>

// Define the type Point
class Point {
// Members below can be accessed from outside the class
public:
	int x;
	Point(int val) : x(val) { std::cout << "Constructed" << std::endl; }
	~Point() { std::cout << "Destructed" << std::endl; }
};

// Program execution starts in main()
int main() {
	// Allocate an object on the heap with new
	Point *p = new Point(5);
	// Print to the console with cout
	std::cout << p->x << std::endl;
	// Free the heap memory with delete
	delete p;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Out of Memory के खिलाफ Guarding करना

new को std::nothrow pass करना (जैसे new(std::nothrow) int[100]) इसे allocation failure पर std::bad_alloc throw करने की बजाय एक null pointer return करने को कहता है, जो उस code में useful है जो safely C++ exceptions handle नहीं कर सकता।

फिर आपको इसे इस्तेमाल करने से पहले returned pointer check करना ज़रूरी है।

उदाहरण: Guarding against Out of Memory

cpp
// Include std::cout and std::cin
#include <iostream>
#include <new>

// Program execution starts in main()
int main() {
	int *arr = new(std::nothrow) int[100];
	// Check whether arr == nullptr
	if (arr == nullptr) {
		// Print to the console with cout
		std::cout << "Allocation failed" << std::endl;
	} else {
		// Print to the console with cout
		std::cout << "Allocation succeeded" << std::endl;
	}
	// Free the heap memory with delete
	delete[] arr;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Rules और Memory Leak Protection

delete के तुरंत बाद एक pointer को nullptr set करना एक dangling pointer रोकता है -- ऐसा pointer जो अभी भी एक address holds करता है लेकिन अब valid memory की ओर point नहीं करता।

एक dangling pointer फिर से इस्तेमाल करना या delete करना undefined behavior है, इसलिए यह simple habit hard-to-debug crashes की एक पूरी class से बचाती है।

उदाहरण: Rules and Memory Leak Protection

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Allocate an object on the heap with new
	int *ptr = new int(5);
	// Free the heap memory with delete
	delete ptr;
	ptr = nullptr;
	// Print to the console with cout
	std::cout << (ptr == nullptr) << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. same pointer पर दो बार delete call करना, जो undefined behavior है।
  2. new[] से allocated memory के लिए delete[] की बजाय delete इस्तेमाल करना।
  3. एक failed allocation handle न करना, जब new std::bad_alloc throw करे।
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.