Fixed memory leak and access violation

Changed order of if statements to check requested array index is within bounds before trying to access it in the matches array.
free()d longopts, memuse checking reported it as a leak