×

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

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.

Structure in C

Why Structure came into the picture? In the actual world, we deal with entities that are collections of objects rather than tiny bits of data like integers, characters, floats, etc. So,...

4 minutes read.

Tower of Hanoi in C

What is Tower of Hanoi? The Tower of Hanoi is a gaming problem that was created in 1883 by a French mathematician named Édouard Lucas. The Tower of Hanoi temple in...

4 minutes read.

Union Program in C

 How should a union be defined?  In C programming, a Union is a method used to organize different data types into groups in the same way that Structures are used. We...

4 minutes read.

String literals in C

Constants refer to the fixed values of the program that may not alter during the compiling ad execution. These fixed values are known as literals. In other words, the values...

3 minutes read.

How to lock top row in Excel

How to lock top row in excel While working with an Excel spreadsheet, the user utilizes the rows and columns to enter the details under various headings. Sometimes while scrolling the...

4 minutes read.

Return array from function in C

Return array from function in C C programming does not require the return to a function of a whole array as an argument. However, you can return a pointer to an array without...

2 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.

What is the main in C?

In the C programming language, "main" is a predefined term or function. It is the primary function in every C program and is in charge of beginning and terminating the...

6 minutes read.

Ftell() Function in C

Ftell(): In File Handling we have some special functions like Ftell(), Fseek(), rewind() etc.. while you are randomly accessing the file these functions play very important role and these functions...

3 minutes read.

One Dimensional Array in C

One Dimensional Array in C: In the C programming language, an array is defined as a collection of one or more values of the same type. Every value present in...

4 minutes read.

Variable Declaration in C

What is a Variable? A Variable is nothing more than a name for a memory place where data/information can be stored. Any alphabet (from a to z or A to Z), the...

4 minutes read.

Functions in C

What is function? Functions are defined as the set of announcements which take inputs, perform operations and provide results. The operation of a function only performs when the function gets called....

4 minutes read.

Queue implementation in C

In the C programming language, the queue is the abstract concept that is similar to stacks. However, it is the part of the data structures that work opposite to that...

4 minutes read.

Nested Loops in C Programming Examples

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 “loop inside the...

6 minutes read.

Difference between C and Java

C programming and Java programming are two of the earliest programming languages. C programming follows a procedural approach whereas Java programming follows an object-oriented approach. Java programming is a part...

3 minutes read.

Ferror() in c

Ferror():  In C, the ferror() function checks assuming there is a blunder in the given stream. The ferror() function is utilized to check for the document blunder on given stream. A return...

4 minutes read.

Heap Sort in C

In this tutorial, we will learn about heap sorting in C language, but before going to Heap sort, we have to know the concept of Complete Binary Tree. What is Complete...

8 minutes read.

How to use atoi() function in C

Introduction: The atoi() is a function used in C programming language. This function converts string characters to the integer value. The atoi() function converts the input string to the return type of...

4 minutes read.

Pure Virtual Function in C

Before knowing about the pure virtual function some of the important points about the virtual function in C++ are given below. In c++ the member function that is defined in the...

4 minutes read.