C Compiler Logo

Invalid Free

Invalid Free

memory

invalid free()

Description

The program attempts to free memory that was not allocated with malloc/calloc or has already been freed.

Common Causes

Example of Error

Error Code

int main() {
    int arr[10];
    free(arr);  // Invalid free - arr is stack-allocated
    return 0;
}

Solution

Only free memory that was allocated with malloc, calloc, or realloc.

Corrected Code

int main() {
    int *arr = malloc(10 * sizeof(int));
    if (arr != NULL) {
        // Use arr
        free(arr);  // Valid free
    }
    return 0;
}

Additional Tips