C++ के Dynamic Arrays
In this page:
data_type *array_name = new data_type[size];
array_name[index] = value;
delete[] array_name;
Dynamic Arrays Allocate करना
एक dynamic array runtime पर new[] से heap पर allocate होता है, जिसका मतलब है इसका size एक variable हो सकता है जो program चलते हुए compute होता है, एक fixed-size stack array के उलट जिसकी length एक compile-time constant होनी चाहिए।
यह इसे सही choice बनाता है जब भी आपको बाद तक पता न हो कि आपको कितना data store करना है।
उदाहरण: Allocating Dynamic Arrays
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare size and set it to 5
int size = 5;
// Allocate an object on the heap with new
int *arr = new int[size];
// Print to the console with cout
std::cout << "Allocated " << size << " ints" << 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 Arrays Initialize करना
आप allocation के बाद एक loop में element-by-element एक dynamic array भर सकते हैं, या value initialization इस्तेमाल कर सकते हैं -- new int[n]() trailing parentheses के साथ -- हर element को unpredictable garbage values के साथ छोड़ने की बजाय automatically zero-initialize करने के लिए।
उदाहरण: Initializing 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]();
// Print to the console with cout
std::cout << arr[0] << " " << arr[1] << " " << arr[2] << 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
Access और Modify करना
Allocate होने के बाद, एक dynamic array एक stack array जैसी ही arr[i] syntax से accessed होता है, क्योंकि new[] जो pointer return करता है वह array के नाम की तरह behave करता है।
Internally वह pointer बस पहले element का memory address store करता है, और indexing इससे एक offset compute करती है।
उदाहरण: Accessing and Modifying
// 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];
arr[0] = 10;
arr[1] = 20;
// Print to the console with cout
std::cout << arr[0] << " " << 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 Arrays Deallocate करना
एक dynamic array से काम खत्म होने पर delete[] exactly एक बार call होना चाहिए, नहीं तो यह जो heap memory occupy करता है वह system को कभी वापस नहीं मिलती।
बाद में pointer को nullptr set करना अच्छी practice है ताकि कोई accidental बाद का इस्तेमाल silently memory corrupt करने की बजाय catch हो जाए।
उदाहरण: Deallocating 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};
// Free the heap memory with delete
delete[] arr;
arr = nullptr;
// Print to the console with cout
std::cout << "Freed" << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Dynamic Arrays Resize करना
C++ arrays in place grow या shrink नहीं हो सकते -- एक को "resize" करने के लिए, आप एक नया, बड़ा array allocate करते हैं, existing elements इसमें copy करते हैं, और old array delete करते हैं।
यह copy-and-replace pattern exactly वही है जो std::vector internally automate करता है, यही वजह है कि manual dynamic arrays से आमतौर पर vector preferred है।
उदाहरण: Resizing 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[2]{1, 2};
// Allocate an object on the heap with new
int *bigger = new int[4];
// Loop: repeat while the condition holds
for (int i = 0; i < 2; i++) bigger[i] = arr[i];
// Free the heap memory with delete
delete[] arr;
arr = bigger;
// Print to the console with cout
std::cout << arr[0] << " " << arr[1] << std::endl;
// Free the heap memory with delete
delete[] arr;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- एक dynamic array को
delete[]की बजायdeleteसे free करना। - allocated range से बाहर पढ़ना या लिखना, जैसे index size के बराबर हो।
- array free करना भूल जाना, जो leak cause करता है।
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: