Skip to Main Content
C Programming: Visual Quickstart Guide
book

C Programming: Visual Quickstart Guide

by Larry Ullman, Marc Liyanage
October 2004
Beginner content levelBeginner
408 pages
9h 24m
English
Peachpit Press
Content preview from C Programming: Visual Quickstart Guide

Resizing a Block of Memory

Sometimes you have to resize a block of dynamic memory after you have already stored data in it. The most common reason for this is the need to grow the block beyond the initial size given to the malloc() call. This often happens if the amount of data to be stored and processed is not known in advance (for example, when you are receiving data over a network connection).

The C library provides the realloc() function for this purpose. It takes two parameters: the address of the memory block to be resized and the desired new size of that block.

void *x, *y;
x = malloc(1000); // Initial block
y = realloc(x, 2000); // Doubled

Retaining the Original Address

Since realloc() can return a NULL pointer if the operation fails, ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Intermediate C Programming

Intermediate C Programming

Yung-Hsiang Lu
Data Structures Using C

Data Structures Using C

Samir Kumar Bandyopadhyay, Kashi Nath Dey

Publisher Resources

ISBN: 0321287630Purchase book