Microsoft Visual C和C ++中具有溢出检查的积分类型值的简单算术运算 [英] Simple Arithmetic Operations on Integral Type Values with Overflow Check in Microsoft Visual C and C++

查看:85
本文介绍了Microsoft Visual C和C ++中具有溢出检查的积分类型值的简单算术运算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Microsoft Visual Studio C和C ++编译器中是否有任何内置方法可以检查计算中出现的溢出,其中包含各种类型,大小的整数,以有符号和无符号形式表示?



在GCC晚期(从5.0.0开始),这些内置函数出现了加法,减法和乘法运算。这些是不同的
__ builtin_< operation> _overflow()函数,请参阅http://gcc.gnu.org/onlinedocs//gcc/Integer-Overflow-Builtins.html链接。



例如,以下一组内置操作允许检查溢出以进行添加:



bool __builtin_add_overflow(type1 a,type2 b,type3 * res);

bool __builtin_sadd_overflow(int a,int b,int * res);

bool __builtin_saddl_overflow(long int a, long int b,long int * res);

bool __builtin_saddll_overflow(long long int a,long long int b,long long int * res);

bool __builtin_uadd_overflow(unsigned int a,unsigned int b,unsigned int * res);
$
bool __builtin_uaddl_overflow(unsigned long int a,unsigned long int b,unsigned long int * res);

bool __builtin_uaddll_overflow(unsigned long long int a,unsigned long long int b,unsigned long long int * res);



上面列表中的第一个函数用于添加整数个任意整数类型(
bool enum 除外),此列表中的后续内置函数专用于具体类型。



例如,我可以通过以下方式检查溢出的情况下溢出:





char a,b,c;

bool o;



o = __builtin_add_overflow(a,b,& c) ;

如果(o)printf("溢出已发生!\ n");



$
在GCC手册中写道,gcc编译器尝试尽可能使用处理器的硬件功能生成代码。如果硬件支持溢出检查给定大小的整数操作数,那么
编译器可能会使用它。例如,对于x86和x86_64处理器,输出代码将使用
OF CF 标志,并包含 jo jc 指令。



Microsoft Visual Studio C / C ++编译器中是否有类似的媒体和扩展允许控制整数算术运算中的溢出?也许它们被实现为内置函数,运算符,宏或编译指示,扩展了标准C和C ++语言的语法

Are there any built-in means in the Microsoft Visual Studio C and C++ compiler to check overflows occurred in computations with integer numbers of various types, size, represented both in signed and unsigned form?

In late GCC (starting from 5.0.0) such built-in functions have appeared for add, subtract and multiply operations. These are different __builtin_<operation>_overflow() functions, see the link http://gcc.gnu.org/onlinedocs//gcc/Integer-Overflow-Builtins.html.

For example, the following set of built-in operations allows to check overflow for addition:

bool __builtin_add_overflow (type1 a, type2 b, type3 *res);
bool __builtin_sadd_overflow (int a, int b, int *res);
bool __builtin_saddl_overflow (long int a, long int b, long int *res);
bool __builtin_saddll_overflow (long long int a, long long int b, long long int *res);
bool __builtin_uadd_overflow (unsigned int a, unsigned int b, unsigned int *res);
bool __builtin_uaddl_overflow (unsigned long int a, unsigned long int b, unsigned long int *res);
bool __builtin_uaddll_overflow (unsigned long long int a, unsigned long long int b, unsigned long long int *res);

The first function in the list above is intended for addition of integer numbers of arbitrary integral types (except of bool and enum), subsequent built-ins in this list are specialized for concrete types.

For example I can check overflow in case of addition executed on chars in the following way:


char a, b, c;
bool o;

o = __builtin_add_overflow (a, b, &c);
if(o) printf("Overflow has occurred!\n");


In the GCC manual it is written that the gcc compiler tries to generate code using hardware capabilities of the processor where possible. If overflow check on the integer operands of the given size is supported by the hardware, probably it will be used by the compiler. For example in case of x86 and x86_64 processors the output code will use OF and CF flags and contain jo and jc instructions.

Are there any similar media and extensions in the Microsoft Visual Studio C/C++ compiler that allow to control overflow in integer arithmetic operations? Perhaps they are implemented as built-in functions, operators, macros or pragmas, extending syntax of the standard C and C++ languages.

推荐答案

查看
Intsafe .h功能
以及
C中的安全整数算法


Take a look at Intsafe.h Functions and also Safe Integer Arithmetic in C


这篇关于Microsoft Visual C和C ++中具有溢出检查的积分类型值的简单算术运算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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