In C and C++, free(my_pointer)
crashes when it is called twice.
Why? There is bookkeeping of every malloc
along with the size. When the f
why does not it check second time when it may not found any allocated size for second free() call
An additional check in the free()
function itself would slow your program down in all correct cases. You should not be doing a double free. Managing memory is your responsibility as a programmer; failure to do so is a programming error. It's part of the philosophy of C: it gives you all the power you need, but as a consequence, makes it easy to shoot yourself in the foot.
Many C runtimes will do some checking in their debug versions, so you'll get a reasonable notification in case you're doing something wrong.