_itoa_s()
should write out one char AND the terminating NUL
char. You want it to write into a buffer with length = 1. So either you get the corruption error because of uninitialized ch_digit
, or _itoa_s()
is not _s (secure) and shmashes your stack by writing behind that one char.
But why not just calculating the ASCII char for base 10 'by hand' instead of using this non portable, MS specific lumber ?
for (int digit = 0; digit < 10; digit++)
{
char ch_digit = '0' + digit; //'0' == 48 == 0x30
}