Correct usage of free() function in C

how free function works in c
free function in c source code
malloc in c
free pointer in c
malloc function in c
realloc in c
when to free memory in c
memory functions in c

I am new in C programming language so can you tell me if this is correct way to do.

for example: program points on buffer and i use that pointer as parameter in free() function. So, what problems can this function cause ?

You call free on pointers which have been assigned memory returned by malloc/calloc/realloc only.


char* ptr=malloc(10); 

// use memory pointed by ptr 
// e.g., strcpy(ptr,"hello");

free(ptr); // free memory pointed by ptr when you don't need it anymore

Things to remember:

  1. Never free memory twice. This can be done: (1) If you call free on ptr twice and value of ptr wasn't changed since first call to free (2) You have two different pointers pointing to same memory; If you call free on one, you are not allowed to call free on the second pointer now too

  2. When you free a pointer you are not even allowed to read its value; e.g., if (ptr) not allowed after freeing unless you initialize ptr to a new value

  3. Of course you should not also dereference freed pointer

  4. As pointed out by @chqrlie, I will also add here that it is also perfectly OK to pass a null pointer to free, which will just do nothing then

Correct usage of free() function in C, The free() function frees the memory space pointed to by a pointer ptr which must have been returned by a pre‐ vious call to malloc() , calloc() or  The C library function void free(void *ptr) deallocates the memory previously allocated by a call to calloc, malloc, or realloc. Declaration. Following is the declaration for free() function. void free(void *ptr) Parameters. ptr − This is the pointer to a memory block previously allocated with malloc, calloc or realloc to be deallocated. If a null pointer is passed as argument, no action occurs.

Think that the computer has a whole bunch of memory not (yet) used by your program. Now you need some more memory and you ask your computer to give you some more (for example, a large buffer). Once you are done with it, you want to return it to the computer.

This memory is called the heap. You ask for memory by calling malloc() and you return it by calling free();

char *buffer;
buffer = malloc(512);           // ask for 512 bytes of memory
if (buffer==NULL) return -1;   // if no more memory available
free(buffer);                 // return the memory again

C library function - free(), This function does not return any value. Example. The following example shows the usage of free() function. Live Demo. #include <stdio. “free” method in C is used to dynamically de-allocate the memory. The memory allocated using functions malloc() and calloc() is not de-allocated on their own. The memory allocated using functions malloc() and calloc() is not de-allocated on their own.

free() function is used to deallocate memory used by one program and move it back to available memory area so that other operating system processes can use that memory location. Also free function takes any type of pointer that points to that memory location. For example:

int a = 10;  // suppose 2 byte is allocated ie location 1000 and 1001

Now this 2 byte of memory belongs to specific problem; hence OS will not give this memory location to another process (memory is now allocated memory not available memory)

 int *ptr =&a;
 /*ptr is pointer variable pointing to 1000  
 as it is int pointer therefore ptr++ will move pointer to 1002*/

Now if we do free(ptr), it will check the pointer type and depending on type free function deallocate memory in this case 2 bytes starting from 1000.

Now interesting point is your data will be there until OS allocates this memory to some other process and that process overwrites it.

Also ptr is pointing to 1000 even after free() function but that memory location does not belong to our program hence ptr pointer has given new name DANGLING POINTER.

*ptr may or may not give the same value therefore it is better to make ptr =null.

C Tutorial – The functions malloc and free » CodingUnit , The function malloc is used to allocate a certain amount of memory during the dis is correct format… whn using dynamic initialization,,, dnt write *TTLSnote[],, allocate memory using malloc() and forget to free it. this also cause the leakage  The malloc function will request a block of memory from the heap. If the request is granted, the operating system will reserve the requested amount of memory. When the amount of memory is not needed anymore, you must return it to the operating system by calling the function free. Take a look at the following example:

From the man page of free() function:

The free() function frees the memory space pointed to by a pointer ptr which must have been returned by a pre‐ vious call to malloc(), calloc() or realloc(). Otherwise, or if free(ptr) has already been called before, undefined behavior occurs. If ptr is NULL, no operation is performed.

You have to use the free() function when you are allocating the memory dynamically.

If you are using that as a static variable then it may lead to unintended behavior.

 char *c=malloc(100);//allocating the 100 bytes of memory for a pointer variable c.

Here after usage of that varaible you can free that allocated memory,


If you are declared a variable like this,

char c= malloc(100);// It is illegeal. And c will have a memory in stack.

If you free this variable,

free(c);//it will lead to system crash. Because your freeing the memory which is in  stack memory.

C Language: free function (Free Memory Block), In the C Programming Language, the free function releases a memory block pointed to by ptr. You can learn more about how we use advertisements here. Keep in mind that the function name points to the beginning address of the executable code like an array name which points to its first element. Therefore, instructions like function_ptr = &Hi_function and (*funptr)(3) are correct.

Dynamic Memory Allocation in C using malloc(), calloc(), free() and , “free” method in C is used to dynamically de-allocate the memory. The memory allocated using functions malloc() and calloc() is not de-allocated on their own. Few Points to Note regarding functions in C: 1) main() in C program is also a function. 2) Each C program must have at least one function, which is main(). 3) There is no limit on number of functions; A C program can have any number of functions. 4) A function can call itself and it is known as “Recursion“. I have written a separate guide

Dynamic Data Structures: Malloc and Free, The block on the right is the block of memory malloc allocated. with the block, you return it to the operating system for recycling by calling the free function. int main() { int *p; p = (int *)malloc(sizeof(int)); if (p == 0) { printf("ERROR: Out of error (another name for zero is NULL and you will see it used throughout C code) . A function can also be referred as a method or a sub-routine or a procedure, etc. Defining a Function. The general form of a function definition in C programming language is as follows − return_type function_name( parameter list ) { body of the function } A function definition in C programming consists of a function header and a function body

Freeing after Malloc (The GNU C Library), Freeing after Malloc (The GNU C Library) you no longer need a block that you got with malloc , use the function free to make The prototype for this function is in stdlib.h . The free function deallocates the block of memory pointed at by ptr . Here is an example of the proper way to free all the blocks in a chain, and the  Take note of the value returned by the program — either 0 for a correct answer or 1 for a wrong answer. You can see that return value in the Code::Blocks output window. The difference between = and == in C programming

  • If you give a pointer to free that was not a pointer returned by malloc/calloc/realloc, Then the code exhibits Undefined Behavior
  • can it lead to spatial memory safety or temporal memory safety errors ?
  • ... What? char c = malloc(100) allocate 100 bytes on the heap, convert the pointer to a char, and then store it to c. Call to free(c) will convert c (a char) to a pointer, and then try to free it, which will lead to system crash because (most of the time) converting a pointer to a char and back will change its value and make it an invalid pointer.