check whether malloc returns a valid pointer