search
Login

Categories


C Books Guide and List
C++ Books Guide and List
Best Java Books

3.8k questions

4k answers

117 comments

10.7k users

0 votes
244 views
asked Expert 13.2k points 26 40 57

1 Answer

0 votes
realloc(): This function is used to increase or decrease the size of any dynamic memory which is allocated using malloc() or calloc() functions.

Syntax: void *realloc(void *ptr, size_t newsize);

The first argument 'ptr' is a pointer to the memory previously allocated by the malloc or calloc functions. The second argument 'newsize' is the size in bytes, of a new memory region to be allocated by realloc. This value can be larger or smaller than the previously allocated memory. The realloc function adjusts the old memory region if newsize is smaller than the size of old memory.

If the newsize is larger than the existing memory size, it increases the size by copying the contents of old memory region to new memory region. The function then deallocates the old memory region. realloc function is helpful in managing a dynamic array whose size may change during execution.

Example: a program that reads input from standard input may not know the size of data in advance. In

this case, dynamically allocated array can be used so that it is possible allocate the exact amount of memory using realloc function.
answered Guru 39.1k points 10 21 31

Related questions

0 votes
1 answer 258 views
0 votes
1 answer 54 views
0 votes
1 answer 102 views
102 views
How do I declare an array of N pointers to functions returning pointers to functions returning pointers to char in c?
asked anonymous
0 votes
1 answer 43 views
43 views
asked denis Sr Member 2.7k points 14 29 59
0 votes
1 answer 448 views
...