Throw std::bad_alloc if malloc/realloc fails