Search code examples
cinteger-overflow

What should my program do when it sees an integer overflow that does not affect the program run?


There is a small program which takes input from users on a prompt. It takes predefined inputs from the users and executes them.

It also displays a number with the prompt indicating the count of the commands :

myprompt 1) usercommand1
...
myprompt 2) usercommand2
...
...
myprompt 3)

I do not expect the user to give more than 65535 commands at a time, so the count is stored as an unsigned short data.

Problem:

I am not sure how the program should handle the case when the user actually crosses this limit of the number of commands. Should I let the count to roll over to 0 (and keep looping) or to stay put at 65535?

I want the program to still function normally, as in take user inputs and process them just as before. Also, the value of count has no effect at all on the command execution.


Solution

  • I looks like you're tackling a problem that might never occur.

    Let's assume your users are quite fast, and it takes them 10 seconds to input a command line. Rollover would happen after 655350 seconds, i.e. approximately seven and a half days.

    Let the counter roll over. If that still troubles you, then take the high path and make it an unsigned long. Then it will only roll over after 1361 years (on 32-bit machines).