check failures of malloc()

Got a segfault because of the assumption that malloc() always succeed, not very pleasant.