On current PC systems, an int
is usually 32 bits or even 64 bits (except one some smaller platforms such as Arduino).
So, probably on your system an int
(or unsigned int
) is larger than 16 bits and 65536 should not overflow. You could easily check this with:
std::cout << sizeof(int) << "\n";
Also, there is no space in num
for the null-terminator:
char* num = new char[5];
sprintf(num, "65536");
So sprintf()
will write a terminating \0
one past your buffer, causing undefined behavior:
There is no way to limit the number of characters written, which means
that code using sprintf
is susceptible to buffer overruns.
This should be changed to:
char* num = new char[6];