C library functions -free()

C Standard Library - <stdlib.h>

Description

In the C language,free()The function is a standard library function, used to release the memory allocated bymalloc(), calloc(), realloc()memory allocated by dynamic allocation functions such as. When the dynamically allocated memory is no longer needed, callingfree()the function can avoid memory leaks and ensure that the program manages memory effectively.

Declaration

Below is the declaration of the free() function.

void free(void *ptr)

Parameter

  • ptr-- The pointer points to a memory block to be freed, which was previously allocated by calling malloc, calloc, or realloc. If the argument passed is a null pointer, no action is performed.

Return Value

This function does not return any value.

Example

The following example demonstrates the usage of the free() function.

Example

#include <stdio.h> #include <stdlib.h> #include <string.h> int main() { char *str; /*Initial memory allocation*/ str = (char *) malloc(15); strcpy(str, "example"); printf("String = %s, Address = %p\n", str, str); /*Reallocate memory*/ str = (char *) realloc(str, 25); strcat(str, ".com"); printf("String = %s, Address = %p\n", str, str); /*Free allocated memory*/ free(str); return(0); }

Let us compile and run the above program, which will produce the following result:

String = example,  Address = 0x7fe4e4c02b10
String = example.com,  Address = 0x7fe4e4c02b10

Notes

  • Freeing the correct memory: Only memory allocated through dynamic memory allocation functions can be freed; memory allocated by other means (for example, local variables or global variables) cannot be freed.

  • Avoiding double freeing: The same memory block cannot be freed multiple times; otherwise, undefined behavior may occur.

  • Handling pointers after freeing: After the memory is freed, the pointer still points to the freed memory location. To avoid a dangling pointer, you can set the pointer toNULL。

    free(array);
    array = NULL;
  • Check for NULL pointer: callfree()Before [freeing], it is best to check whether the pointer is NULL to ensure program stability.

    if (array != NULL) {
        free(array);
        array = NULL;
    }

By using the free() function correctly, dynamically allocated memory can be managed properly, avoiding memory leaks and dangling pointer problems, and improving the stability and reliability of the program.


Memory Leaks and Dangling Pointers

  • Memory leak: If dynamically allocated memory is not freed or its reference is lost, the memory can no longer be used by the program, resulting in a memory leak.
  • Dangling pointer: A pointer that points to freed memory is called a dangling pointer. If a dangling pointer is accessed again, it leads to undefined behavior, which may cause program crashes or data corruption.

In C, after calling free() to release dynamically allocated memory, the pointer still holds the previously allocated address, which leads to a dangling pointer. A dangling pointer is a pointer that points to freed memory. Dereferencing it leads to undefined behavior.

To avoid the dangling pointer problem, the usual practice is to set the pointer to NULL after calling free(). This ensures that the program does not mistakenly use freed memory.

Example

#include <stdio.h>
#include <stdlib.h>

int main() {
    // dynamically allocate memory
    char *str = (char *)malloc(100 * sizeof(char));
    if (str == NULL) {
        printf("Memory allocation failed\n");
        return 1;
    }

    // Use allocated memory
    snprintf(str, 100, "Hello, World!");
    printf("%s\n", str);

    // Release dynamically allocated memory
    free(str);
   
    // set the pointer to NULL to avoid a dangling pointer
    str = NULL;

    // now str is NULL, safely avoiding the dangling pointer problem
    if (str == NULL) {
        printf("str is NULL\n");
    }

    return 0;
}

In this example, the str pointer is set to NULL after the free() call, thereby avoiding the dangling pointer problem. If you attempt to use this pointer again, since it is NULL, you can avoid undefined behavior through a conditional check.

C Standard Library - <stdlib.h>

other extensions