C++ में new और delete
In this page:
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
- same pointer पर दो बार
deletecall करना, जो undefined behavior है। new[]से allocated memory के लिएdelete[]की बजायdeleteइस्तेमाल करना।- एक failed allocation handle न करना, जब
newstd::bad_allocthrow करे।
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: