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

C++ के Dynamic Arrays

एक dynamic array boxes की एक row है जिसका size आप program चलते वक्त choose करते हैं, ज़रूरत जितने lockers order करने की तरह। खत्म होने पर आपको इसे free करना ज़रूरी है।
Syntax
cpp
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

cpp
// 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;
}

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

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]();
	// 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;
}

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

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];
	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;
}

Dynamic Arrays Deallocate करना

एक dynamic array से काम खत्म होने पर delete[] exactly एक बार call होना चाहिए, नहीं तो यह जो heap memory occupy करता है वह system को कभी वापस नहीं मिलती।

बाद में pointer को nullptr set करना अच्छी practice है ताकि कोई accidental बाद का इस्तेमाल silently memory corrupt करने की बजाय catch हो जाए।

उदाहरण: Deallocating 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};
	// 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;
}

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

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[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;
}
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. एक dynamic array को delete[] की बजाय delete से free करना।
  2. allocated range से बाहर पढ़ना या लिखना, जैसे index size के बराबर हो।
  3. array free करना भूल जाना, जो leak cause करता है।
🔒

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.