The behavior on free() for an invalid address is undefined because it is unpredictable. In a low-level language like C, you cannot tell if the address is "valid" or "invalid". And there are many ways in which an address can be invalid. Already freed is only one of the following cases: an address not assigned by malloc, data is not really an address (for example, some integer or character string passed to a pointer), data corruption, etc.
If you are only interested in the “freed” case, you can try to track the allocation and freeing of memory (for example, using the malloc() and free() user shell), but this will probably only make your code more complex and even more error prone.
The simple answer is no, there is no way to check if the address is invalid. Live with it or switch to a higher level language (like Python) when you don't care about memory addresses at all.
source share