当我的程序看到不影响程序运行的整数溢出时应该做什么?

发布于 2024-10-16 05:42:38 字数 436 浏览 7 评论 0原文

有一个小程序可以根据提示接受用户的输入。它从用户那里获取预定义的输入并执行它们。

它还显示一个数字,并带有指示命令计数的提示:

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

我不希望用户一次发出超过 65535 个命令,因此 count 存储为 unsigned Short数据。

问题:

我不确定当用户实际超过命令数量限制时程序应如何处理这种情况。我应该让计数翻转到 0(并保持循环)还是保持在 65535?

我希望程序仍然正常运行,就像以前一样接受用户输入并处理它们。此外,count 的值对命令执行没有任何影响。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

咿呀咿呀哟 2024-10-23 05:42:38

我看起来你正在解决一个可能永远不会发生的问题。

假设您的用户速度非常快,他们需要 10 秒才能输入命令行。翻转将在 655350 秒后发生,即大约七天半

让计数器翻过来。如果这仍然困扰您,那么请采用高路径并将其设为 unsigned long。然后它只会在 1361 年之后滚动(在 32 位机器上)。

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).

寂寞笑我太脆弱 2024-10-23 05:42:38

如果您问自己这个问题,则意味着您应该采用简单的方法:将计数器设为 unsigned int

如何处理该限制很大程度上取决于该计数器的用途。我的感觉是它没有用于任何真正有趣的事情,所以你的问题有点没有实际意义。无论您做出哪种选择,它仍然可以正常工作。

另一方面,如果此计数器作为某种真实用途,您应该询问该计数器的用户正确的操作方法:两者都有一些优点和缺点(计数器及时返回或停止),因此您的用户可能会感到惊讶。

您忘记提及其他选择:终止您的程序。或者删除限制并使用某种形式的大整数(例如 GMP lib),但这听起来有点矫枉过正。

请注意,DNS 选择以 2^32 环绕序列号。这使得它可以永远使用。计数器的用户应该检测到溢出。 RFC 1982

If you ask yourself this question it means you should go the easy way: make the counter an unsigned int.

How to handle the limit is very dependant on what this counter is used for. My feeling is that it is not used for any really interesting thing so your question is kind of moot. Whichever choice you make it will still work correctly.

On the other hand if this counter as some real use you should ask the user of this counter the correct way to proceed: both have some pros and cons (either counter going back in time or stalling) so your user risk being surprised.

You forgot to mention other alternatives: terminate your program. Or remove the limit and use some form of big integers (GMP lib for example) but this souns overkill.

Note that the DNS choose to wraparound the serial number at 2^32. This makes it usable forever. Users of the counter are supposed to detect the overflow. RFC 1982

情绪少女 2024-10-23 05:42:38

老实说这个:

我希望程序仍然可以运行
通常,如接受用户输入和
像以前一样处理它们。另外,
count 的值根本没有影响
命令执行。

回答你自己的问题,如果它根本没有影响,那么就让它再次从 0 开始。

To be honest this:

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.

answers your own question, if it has no effect at all then just let it start on 0 again.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文