Developer community
0 votes
244 views
asked by Expert (13.2k points) 27 41 59 | 244 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 34

Related questions

0 votes
1 answer
asked by rajesh Guru (39.1k points) 10 22 34 | 315 views
0 votes
1 answer
asked by smith Geek (15.3k points) 17 41 61 | 59 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

Categories


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

3.8k questions

4k answers

117 comments

10.7k users

3,751 questions
4,046 answers
117 comments
10,709 users