Eliminating memory errors in C