malloc function almost completed. should be complete after figuring out how to make sure the right memIndex is set