The program will run normally when I try to visit an address which is already freed. Is there some goods to avoid? like some function named have_alloca(void *p) can return whether p is a valid address.
I know that valgrind with tool=memcheck can do that. But I want to know if I can avoid it from code.
here is a simple example:
#include <stdlib.h>
int main(int argc, char *argv[])
{
int *p = malloc(sizeof(int) * 10);
free(p);
*(p + 1) = 100;
return 0;
}
Why I can visit a invalid address? and the program can compile and run without any warns.
BTW: Linux.
No, You will have to take care of this yourself.
Technically, writing to an memory location that does not belong to you is Undefined Behavior from the perspective of the language standard.
The languages C and C++ provide you the flexibility to reach addresses of variables and play around with them at the expense that it is your responsibility that those addresses contain valid values owned by your variable/object.
The rationale is:
“With greater power comes greater responsibility”
So the responsibility is yours to take.