Fix a problem in serial.c

The current counter was compared with the wrong one.
Move the computation of the neighbourhood function

Now it is computed at the beginning of the loop.
Add check_ptr.h

Add checking of pointers returned by calls to malloc