I'm not sure what the purpose of the buffer is if you never actually use it as a complete array; seems like an extra memory allocation to me. Not to mention it's writing (and reading) beyond the buffer bounds when dy == 23 (your buffer of char[23] has indices from 0 to 22).