Developer community
0 votes
asked by Expert (13.2k points) 27 43 61 | 279 views

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 by Guru (39.1k points) 10 22 36

Related questions

0 votes
1 answer
asked by rajesh Guru (39.1k points) 10 22 36 | 375 views
0 votes
1 answer
asked by smith Geek (15.3k points) 17 43 67 | 63 views
0 votes
1 answer
asked by denis Sr Member (2.7k points) 14 29 62 | 42 views
0 votes
1 answer
Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true


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

3.8k questions

4k answers


10.7k users

3,754 questions
4,046 answers
10,709 users