রিয়েললক ফাংশনটি মেমরি ব্লকের আকার পরিবর্তন করতে ব্যবহৃত হয় যা আগে malloc বা calloc দ্বারা বরাদ্দ করা হয়।
এখানে সি ল্যাঙ্গুয়েজে রিয়েললকের সিনট্যাক্স রয়েছে,
void *realloc(void *pointer, size_t size)
এখানে,
পয়েন্টার − যে পয়েন্টারটি malloc বা calloc দ্বারা পূর্বে বরাদ্দ করা মেমরি ব্লক নির্দেশ করছে৷
আকার - মেমরি ব্লকের নতুন আকার।
এখানে C ভাষায় realloc() এর একটি উদাহরণ রয়েছে,
উদাহরণ
#include <stdio.h>
#include <stdlib.h>
int main() {
int n = 4, i, *p, s = 0;
p = (int*) calloc(n, sizeof(int));
if(p == NULL) {
printf("\nError! memory not allocated.");
exit(0);
}
printf("\nEnter elements of array : ");
for(i = 0; i < n; ++i) {
scanf("%d", p + i);
s += *(p + i);
}
printf("\nSum : %d", s);
p = (int*) realloc(p, 6);
printf("\nEnter elements of array : ");
for(i = 0; i < n; ++i) {
scanf("%d", p + i);
s += *(p + i);
}
printf("\nSum : %d", s);
return 0;
} আউটপুট
Enter elements of array : 3 34 28 8 Sum : 73 Enter elements of array : 3 28 33 8 10 15 Sum : 145
উপরের প্রোগ্রামে, মেমরি ব্লক calloc() দ্বারা বরাদ্দ করা হয় এবং উপাদানগুলির যোগফল গণনা করা হয়। এর পরে, realloc() মেমরি ব্লকের আকার 4 থেকে 6 করে এবং তাদের যোগফল গণনা করছে।
p = (int*) realloc(p, 6);
printf("\nEnter elements of array : ");
for(i = 0; i < n; ++i) {
scanf("%d", p + i);
s += *(p + i);
}