fixed c code, allocate memory each iteration of while loop