implementation of malloc() and free() with error detection