Programming Tutorials

Using realloc() Function in C

By: Charles in C Tutorials on 2007-10-03  

The realloc() function changes the size of a block of memory that was previously allocated with malloc() or calloc(). The function prototype is

void *realloc(void *ptr, size_t size);

The ptr argument is a pointer to the original block of memory. The new size, in bytes, is specified by size. There are several possible outcomes with realloc():

  • If sufficient space exists to expand the memory block pointed to by ptr, the additional memory is allocated and the function returns ptr.
  • If sufficient space does not exist to expand the current block in its current location, a new block of the size for size is allocated, and existing data is copied from the old block to the beginning of the new block. The old block is freed, and the function returns a pointer to the new block.
  • If the ptr argument is NULL, the function acts like malloc(), allocating a block of size bytes and returning a pointer to it.
  • If the argument size is 0, the memory that ptr points to is freed, and the function returns NULL.
  • If memory is insufficient for the reallocation (either expanding the old block or allocating a new one), the function returns NULL, and the original block is unchanged.

Listing below demonstrates the use of realloc().

Using realloc() to increase the size of a block of dynamically allocated memory.

1: /* Using realloc() to change memory allocation. */
3: #include <stdio.h>
4: #include <stdlib.h>
5: #include <string.h>
7: main()
8: {
9:     char buf[80], *message;
11:     /* Input a string. */
13:     puts("Enter a line of text.");
14:     gets(buf);
16:     /* Allocate the initial block and copy the string to it. */
18:     message = realloc(NULL, strlen(buf)+1);
19:     strcpy(message, buf);
21:     /* Display the message. */
23:     puts(message);
25:     /* Get another string from the user. */
27:     puts("Enter another line of text.");
28:     gets(buf);
30:     /* Increase the allocation, then concatenate the string to it. */
32:     message = realloc(message,(strlen(message) + strlen(buf)+1));
33:     strcat(message, buf);
35:     /* Display the new message. */
36:     puts(message);
37:     return(0);
38: }
Enter a line of text.
This is the first line of text.
This is the first line of text.
Enter another line of text.
This is the second line of text.
This is the first line of text.This is the second line of text.

ANALYSIS: This program gets an input string on line 14, reading it into an array of characters called buf. The string is then copied into a memory location pointed to by message (line 19). message was allocated using realloc() on line 18. realloc() was called even though there was no previous allocation. By passing NULL as the first parameter, realloc() knows that this is a first allocation.

Line 28 gets a second string in the buf buffer. This string is concatenated to the string already held in message. Because message is just big enough to hold the first string, it needs to be reallocated to make room to hold both the first and second strings. This is exactly what line 32 does. The program concludes by printing the final concatenated string.

Add Comment

* Required information


No comments yet. Be the first!

Most Viewed Articles (in C )

Latest Articles (in C)