• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » DS » Basic » What is the difference between realloc() and free()
Next →
← Prev

What is the difference between realloc() and free()

By Dinesh Thakur

The free subroutine frees a block of memory previously allocated by the malloc subroutine. Undefined results occur if the Pointer parameter is not a valid pointer. If the Pointer parameter is a null value, no action will occur.

The realloc () function is used to allocate memory and has the following prototype:
void * realloc (void * ptr, unsigned int num);
The function modifies the size of the previously allocated memory pointed to by *ptr to that specified by a. The value of a may be higher or lower than the original. A pointer to the block is returned because realloc () may need to move the block to increase its size. If this occurs, the old block content is copied into the new block, and no information is lost. If ptr is null, allocates size bytes and returns a pointer; if size is zero, the memory pointed to by ptr is released. If there is not enough memory to allocate, a null pointer is returned and the original block is left unchanged.

To avoid waste of memory or the memory leak (memory leaks), then we should do the reallocation of the spaces memory previously allocated by function malloc (), calloc () or realloc (). In the C language, this process will be carried out by using function free () which has the form of a pointer parameter. Here is the prototype of function free ().

void *free(void *p);

p must be a pointer here previously allocated using function malloc (), calloc () or realloc (). Here is an example of use function free ().

The use of pointers will usually cause memory leaks, the event where there is room memory is wasted in vain because the memory space can no longer in access to allocated. This memory leak can result in programs or our operating system be damaged or have a hang. Here is an example of the syntax which will result in a memory leak.

#include <stdio.h>
#include<conio.h>
int main (void)
{
   /* Declare a pointer P which would point to the type of data int */
   void *P;
   int x = 10;
   double y = 15.3;
   clrscr();
   /* Ordered memory space to put the type int */
   P = (int *) malloc (sizeof (int));
    /* Ordered pointer P to point to the address of variable x */
   P = & x;
    /* Display the value contained in the pointer P */
   printf ("The value of P \t =% p \n", P);
   printf ("Value *P \t =% d \n \n", * ((int *) P));
   /* Ordered memory space to put the type double */
   P = (double *) malloc (sizeof (double));
   /* Ordered pointer P to point to the address of variable y */
   P = & y;
   /* Display the value contained in the pointer P */
   printf ("The value of P \t =% p \n", P);
   printf ("Value *P \t =% .1f \n", * ((double *) P));
   return 0;
}
At first glancethe above programasproperlyandifimplementedwouldalsoprovide the following result.

     Free Memory

In the above program, initially we booked a space (memory address) to placing a value of type int (ie the address FFF2) then record it into a pointer P. After the process of the pointer, in we booked a room on back to hold the value of type double (ie FFEA address) and record it back to the pointer P. This resulted in pointer P who was appointed address FFF2 move to point to FFEA address, so the address is no longer accessible FFF2 will result in the waste of memory space is wasted. This kind of incident called with a memory leak.

To avoid this, we should free FFF2 address before ordering pointer P to point to the new address, namely by using the function free (). Thus, the syntax of the program should write as follows.

#include <stdio.h>
#include<conio.h>
int main (void)
{
   void * P;
   int x = 10;
   double y = 15.3;
   clrscr();
   P = (int *) malloc (sizeof (int));
   P = & x;
   printf ("The value of P \t =% p \n", P);
   printf ("Value *P \t =% d \n \n", * ((int *) P));
   /* Free pointer P with the function free () */
   free (P);
   P = (double *) malloc (sizeof (double));
   P = & y;
   printf ("The value of P \t =% p \n", P);
   printf ("Value *P \t =% .1f \n", * ((double *) P));
   return 0;
} 

     Free() Function

You’ll also like:

  1. What is difference between malloc()/free() and new/delete
  2. What is the purpose of realloc( )
  3. Using free() Function in C
  4. You have two pairs : new() and delete() and another pair : alloc() and free(). Explain differences between eg. new() and malloc()
  5. What is Context Free Grammars? Compiler Design
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

Data Structure

Data Structure Tutorials

  • DS - Home
  • DS - Sorting
  • DS - Shortest Path Algorithm
  • DS - free() Function
  • DS - Graph Traversals
  • DS - Linear Search
  • DS - Heap Sort
  • DS - Searching
  • DS - Linked Lists
  • DS - Algorithms
  • DS - Bubble Sort
  • DS - Quick Sort
  • DS - Binary Search
  • DS - realloc() Vs free()
  • DS - Steps to Plan Algorithm
  • DS - Record Structure
  • DS - Single Linked List
  • DS - Purpose of realloc()
  • DS - Use malloc()
  • DS - calloc() Vs malloc()

Other Links

  • Data Structure - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2023. All Rights Reserved.