如何检测Long类型的溢出 [英] How to detect Overflow of Long type

查看:182
本文介绍了如何检测Long类型的溢出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

任何人都可以帮助我检测到long数据类型的溢出.我是C ++.
我不想采用b <b的算术方式. (b + C),然后发生溢流.
是否有任何方法或方法可以为我检测溢出?

问候,
Saurabh

Hi guys,

Can any one help me with detecting an overflow with data type long. I C++.
i dont want to go for the arithmetic way that if b < (b+C) then over flow occurred.
Is there any Method or falg that will detect the overflow for me.?

With Regards,
Saurabh

推荐答案

Idle63为长"数据类型的替代方法提供了一个很好的答案.
但是更直接的答案是,C语言没有内置任何方法或标志可以为您检测到此溢出.

C语言设计为在尽可能接近金属的地方运行.对原始类型进行广泛的边界验证,这将严重影响代码性能,并且留给软件工程师练习.此外,边界检查的自定义要求针对下溢和上溢进行比较的其他变量.这将为原始数据类型增加额外的复杂性.
Idle63 posted a good answer for an alternative to a ''long'' data type.

But a more direct answer is that, there is no method or flag, built into the C language that will detect this overflow for you.

The C language was designed to operate as close to the metal as possible. Widespread bounds validation for primitive types, would severely impact code performance, and is left as an exercise for the software engineer. Further, customization of bounds checking, requires additional variables to compare against, for underflow and overflow. This would add additional complexity to primitive data types.


1.如果您了解汇编程序,则可以在未设置溢出标志时使用操作数JO和JNO进行跳转.这将是x86平台上的通用答案.

2.如果您不理解汇编程序,则只需使用安全类型(如Safe Integer)-请参阅在MSDN中使用的安全类型.
1. If you understand assembler, then you can use operands JO and JNO to jump when overflow flag is set or not, accordingly. This would be the universal answer on x86 platform.

2. If you do not understand assembler, then just use safe types like Safe Integer - see them used in MSDN.


您可以使用SafeInt. http://msdn.microsoft.com/en-us/library/dd570023(VS .100).aspx [ ^ ]

但是性能可能会下降.
问候
You can use SafeInt. http://msdn.microsoft.com/en-us/library/dd570023(VS.100).aspx[^]

But probably it perfromance goes down.
Regards


这篇关于如何检测Long类型的溢出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆