×

Use of free() function in C

Introduction

The free() function uses in C programming language. The free() function in the C programming language uses to release or deallocate the memory blocks; these blocks are previously allocated by calloc(), malloc() or realloc() functions.

This function is free the memory block. If you want to use any memory location that is not accessible, then you can call the free() function to free the required memory location.

In the C programming language, the memory for the variable is reallocated during the compile time, but the dynamic memory for the variable is not deallocated during the compile time.

It is allocated separately. The free () function used a pointer in a free memory location.

Syntax of the free() function in C language:

Syntax means the order or arrangement in the program. The syntax is also known as the function prototype. Every function has a syntax value. The syntax of the free() function is: 

void free (void* ptr);

The free() function is used to pointer the free memory location.

Parameters of the free() function in C language:

The main parameter of the free() function is ptr. Str represents the pointer. The pointer uses to point to the free memory location or block. This blocks are previously allocated by calloc() function, malloc() function or realloc() functions. When the null pointer passes as an argument, then no value occurs.

Returns Value of the free() function in C language:

The free function is mainly used to pointer the free memory location. This function does not return any value. It's only used to find the free memory block.

Header file of the free() function in C language :

Every program contains some header files. For the run of any program, you must need a header file. Without a header file, you can't run any programs. So, we must produce the header file in every program. It is a file with an extension. The free() function is a function which is pointing the free memory address.

The primary use of the header file is to propagate the declaration of code files. The free() function uses as a library function. The header file of the free() function in the C language is <stdlib.h>.

It writes in the program as #include <stdlib.h>

Example 1 –

Here we were given an example of the free() function using the C language:

//header file initialize

#include<stdio.h>
#include<stdlib.h>
//main code of this program
int main() 
{
int*ptr=malloc(20*sizeof(*ptr));
if(ptr!=NULL)
{
*(ptr+4) = 40;
//print the result
 printf("The value of the 2nd integer = %d",*(ptr+2));
}
free(ptr);
return (0);
}

Output: We compile and run the above program.The result of this above program is:

The value of the 2nd integer = 40

To better understand, we give another example of the free() function.

Example 2 –

Here we given a example of the free() function using C language:

//header file initialize

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main ()
{
char *str;
//memory allocation
str = (char *) malloc(25);
strcpy (str, “Tutorial and example”);
printf (“String = %s, Address = %u\n”, str, str);
//reallocating memory
str = (char *) realloc(str, 35);
strcat (str, “.com”);
printf (“String = %s, Address = %u\n”, str, str);
//deallocated memory
free (str);
return (0);
}

Output: We compile and run the above program.The result of this above program is:

String = Tutorial and example, Address = 985735479
String = Tutorial and example.com, Address = 985735479

In the above two programs, we have used the free () function; by the programs, we briefly discussed how to write any C language program using the free () function. The free () function is a library function.

So we <stdlib.h> header file in this program. 

In a program, if you want to use any memory location but it was not free at that time, then you can call the free() function to free the required memory location. In the above programs, we use stdio.h, stdlib.h and string.h header files.

The primary use of the header file is to propagate the declaration of code files. The output of the two programs is dependent on the memory location. We also share the output of the above two programs. 


Related Topics

Sum of digits in C++

Sum of digits in C++ There are various ways to find the sum of the digits of a number in C++. We can use containers like arrays or other simple cases...

4 minutes read.

Types of Function in C

A function is a piece of code that accomplish a certain operation for a specific task. There are two types of functions in C: System-defined function (Library Function)User-defined function 1. System defined Function...

8 minutes read.

C Function Argument and Return Values

Functions in the C programming language are declared to avoid repeatedly writing a performable block of code; it is the same in any programming language. When it comes to the...

3 minutes read.

Find median of 1D array using function in C

Introduction: Here, we discuss how we can find the median of a 1D array using a function in C. The median is the value in the middle of the sorted...

3 minutes read.

Fseek Function in C

The fseek function is a function in the C standard library that changes the position of the file pointer in a stream-oriented file. It is typically used to move the...

3 minutes read.

How to Merge Array in C?

Introduction Merging arrays is a common task for many developers but might be difficult for beginners of C programming. Fortunately, our guide will show you how to quickly merge arrays in...

6 minutes read.

Nested loop in C

Definition of Nested Loop A nested loop is generally used when we want to run a loop statement inside another loop statement. This kind of loop is also known as a...

3 minutes read.

Find Union and Intersection of Two Arrays in C

Union You can find the union of the two sorted arrays using the join merge method on arr1[] and arr2[]. Use two index variables i and j with initial values i =...

4 minutes read.

User Defined Functions in C

C Functions: A function is defined as a block of code that is used to perform some specific task. In functions, we use flower brackets ({}) which contain the set of programming...

5 minutes read.

Passing Array to Function in C

Need to pass Arrays? The need to pass arrays to a function arises when we need to pass a list of values to a given function. During the course of our programming...

4 minutes read.

C Programming Tutorial

What is C Programming C is most popular and widely used computer programming language. It was developed by Dennis Ritchie in 1972 at the Bell Lab. It is used to develop system application software. There...

8 minutes read.

Two-Dimensional array in C

What is an Array? The collection of a fixed number of elements of homogeneous data types that are stored in contiguous locations in the memory is known as an array. Only...

6 minutes read.

Difference between Scope and Lifetime in C

In this article, we will discuss about the key differences between scope and lifetime in C, including their definitions, applications, and how they relate to one another. By the end...

8 minutes read.

Union in C

Union is a user-defined data type that is used to hold the different types of elements like structure. In union, all members share the same memory location. Syntax: union [union name] { ...

1 minute read.

Banker’s Algorithm in C

Introduction Before doing a "s-state" check to look for potential activities and deciding if allocation should be allowed to continue, the banker's algorithm, a resource allocation and deadlock avoidance algorithm, tests...

5 minutes read.

Socket Programming in C

Socket programming is a process that connects the two or more nodes on a networking communication with respective to each other. One of the sockets that determines the socket (I.e.,...

5 minutes read.

How to convert a number to words in C

There are many ways available for converting an integer or a number to its word form. For example, consider a number as 12345. This number can be represented in two...

3 minutes read.

C Program Swap Numbers in cyclic order Using Call by Reference

The three integers that the user entered in cyclical sequence are swapped in this tutorial using call by reference. C Program: #include <stdio.h> void cyclic_Swap ( int *X, int *Y, int *Z ); int...

2 minutes read.

Program to Find Mode of an Array in C

Mode is the highest occurring value or number in a given set of data elements. In a group of data values, a value that occurs most frequently is considered to...

3 minutes read.

Pointers in C

Pointers in C: In the C programming language, a pointer is a pointer variable that points to the address of the other variable. It also stores the address of the...

4 minutes read.