Hello everyone, I have a heap allocated array arr. The allocation in arr is freed by using a pointer ptr. Therefore, if(arr) statement should return a false value. But practically it returns a true value and go ahead to call the free(arr) to free up the allocated space again. Resultantly, the program crashed.
Code:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int* arr = (int*)malloc(3 * sizeof(*arr));
arr[0] = 12; arr[1] = 61; arr[2] = 40;
int* ptr = arr;
free(ptr);
ptr = nullptr;
if(arr)
free(arr);
arr = nullptr;
return 0;
}