char* x = malloc(512);
memset(x, 0, 513);
free(x);
Why is this crashing the program? It is saying free(): invalid pointer: 0x0000000000614010
That doesn’t make any sense, if anything it should be ruining some other random part of the program because it writes into undefined memory space.
You’ve caused undefined behaviour, so anything can happen. In this case, it’s possible that your
free()implementation might be checking for some information in the space immediately after your buffer. Since you overwrote it, it’s game over.