C का realloc()
In this page:
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()
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Allocated Memory Expand करना
क्योंकि system को memory में कहीं और एक बड़ा contiguous free block ढूंढना पड़ सकता है, realloc() pointer का actual address बदल सकता है -- original block के कोई भी पुराने pointers या references एक successful call के बाद invalid हो जाते हैं।
उदाहरण: Expanding Allocated Memory
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Allocated Memory Shrink करना
नए छोटे size से आगे की bytes discard हो जाती हैं, लेकिन scratch से free करने और reallocate करने के उलट, in place shrink करना (जब possible हो) बची हुई data को एक नई location पर copy करने का overhead avoid करता है।
उदाहरण: Shrinking Allocated Memory
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
realloc() के साथ Best Practices
क्योंकि हर reallocation में potentially पूरे block के contents को एक नए address पर copy करना शामिल है, एक array को एक loop में एक बार में एक element बढ़ाना इसे कम बार बड़े chunks में (जैसे capacity double करके) बढ़ाने से कहीं ज़्यादा धीमा है।
उदाहरण: Best practices with realloc()
// 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;
}
Login to try C/C++/Java/PHP code in the editor
reallocका result सीधे वापस original pointer को assign करना, जोreallocfail होने औरNULLreturn करने पर पुराना block खो देता है।reallocके बाद पुराना pointer इस्तेमाल करना, जब block move हो चुका हो सकता है।- result इस्तेमाल करने से पहले
reallocकोNULLके लिए check करना भूल जाना।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: