replace malloc by emalloc, added test case for offset