code for sequential moments with a malloc problem