← Back to C Course | Chapter 9: Memory Management | Lesson 5 of 8

C का realloc()

realloc आपके पहले borrow किए हुए memory का size बदलता है, अपने table को बड़े या छोटे से बदलने जैसा। यह आपका data एक नई जगह move कर सकता है।
Syntax
c
pointer_name = (data_type *) realloc(pointer_name, new_count * sizeof(data_type));

realloc() का Introduction

जब आपको array का final size पहले से नहीं पता होता तो यह पसंदीदा tool है -- उदाहरण के लिए, एक file से unknown संख्या में lines पढ़ना और जैसे-जैसे आगे बढ़ें एक dynamic array को fit करने के लिए बढ़ाना, हर बार भरने पर इसकी capacity double करते हुए।

उदाहरण: Introduction to realloc()

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include general utilities (malloc, free, exit)
#include <stdlib.h>
// Program execution starts in main()
int main() {
	// Allocate memory on the heap
	int *arr = malloc(2 * sizeof(int));
	arr = realloc(arr, 4 * sizeof(int));
	arr[3] = 99;
	// Print formatted text to the screen
	printf("%d", arr[3]);
	// Release the heap memory so it can be reused
	free(arr);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Allocated Memory Expand करना

क्योंकि system को memory में कहीं और एक बड़ा contiguous free block ढूंढना पड़ सकता है, realloc() pointer का actual address बदल सकता है -- original block के कोई भी पुराने pointers या references एक successful call के बाद invalid हो जाते हैं।

उदाहरण: Expanding Allocated Memory

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include general utilities (malloc, free, exit)
#include <stdlib.h>
// Program execution starts in main()
int main() {
	// Allocate memory on the heap
	int *arr = malloc(2 * sizeof(int));
	arr[0] = 1; arr[1] = 2;
	arr = realloc(arr, 5 * sizeof(int));
	// Print formatted text to the screen
	printf("%d", arr[0]);
	// Release the heap memory so it can be reused
	free(arr);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Allocated Memory Shrink करना

नए छोटे size से आगे की bytes discard हो जाती हैं, लेकिन scratch से free करने और reallocate करने के उलट, in place shrink करना (जब possible हो) बची हुई data को एक नई location पर copy करने का overhead avoid करता है।

उदाहरण: Shrinking Allocated Memory

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include general utilities (malloc, free, exit)
#include <stdlib.h>
// Program execution starts in main()
int main() {
	// Allocate memory on the heap
	int *arr = malloc(5 * sizeof(int));
	arr = realloc(arr, 2 * sizeof(int));
	// Print formatted text to the screen
	printf("%zu bytes", 2 * sizeof(int));
	// Release the heap memory so it can be reused
	free(arr);
	// Return 0 to signal that the program finished successfully
	return 0;
}

realloc() Failures को Safely Handle करना

यही failure behavior है जिस वजह से आपको कभी सीधे ptr = realloc(ptr, newSize) नहीं लिखना चाहिए -- अगर यह fail हो और NULL return करे, आपने अभी-अभी original valid block का अपना इकलौता reference overwrite कर दिया, एक memory leak cause करते हुए।

उदाहरण: Handling realloc() failures safely

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include general utilities (malloc, free, exit)
#include <stdlib.h>
// Program execution starts in main()
int main() {
	// Allocate memory on the heap
	int *arr = malloc(2 * sizeof(int));
	int *temp = realloc(arr, 4 * sizeof(int));
	// Check whether temp != NULL
	if (temp != NULL) {
		arr = temp;
	}
	// Print formatted text to the screen
	printf("%d", arr != NULL);
	// Release the heap memory so it can be reused
	free(arr);
	// Return 0 to signal that the program finished successfully
	return 0;
}

realloc() के साथ Best Practices

क्योंकि हर reallocation में potentially पूरे block के contents को एक नए address पर copy करना शामिल है, एक array को एक loop में एक बार में एक element बढ़ाना इसे कम बार बड़े chunks में (जैसे capacity double करके) बढ़ाने से कहीं ज़्यादा धीमा है।

उदाहरण: Best practices with realloc()

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include general utilities (malloc, free, exit)
#include <stdlib.h>
// Program execution starts in main()
int main() {
	// Declare capacity and set it to 2
	int capacity = 2;
	// Allocate memory on the heap
	int *arr = malloc(capacity * sizeof(int));
	capacity *= 2;
	arr = realloc(arr, capacity * sizeof(int));
	// Print formatted text to the screen
	printf("%d", capacity);
	// Release the heap memory so it can be reused
	free(arr);
	// 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. realloc का result सीधे वापस original pointer को assign करना, जो realloc fail होने और NULL return करने पर पुराना block खो देता है।
  2. realloc के बाद पुराना pointer इस्तेमाल करना, जब block move हो चुका हो सकता है।
  3. result इस्तेमाल करने से पहले realloc को NULL के लिए check करना भूल जाना।
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 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.