在 C/C++ 中检测有符号溢出

问题描述乍一看,这个问题可能与How检测整数溢出?,但实际上有很大不同.Atfirstglance,thisquestionmayseemlikeaduplicateofHowtodetectintegeroverflow?,howeveritisactuallysignific

发布:2022-10-16 标签:c++csignedundefined-behaviorinteger-overflow


是否存在“未定义行为"?真的允许*任何事情*发生吗?

问题描述未定义行为"的经典杜撰示例;当然是鼻恶魔".—物理上不可能,无论C和C++标准允许什么.Theclassicapocryphalexampleof"undefinedbehavior"is,ofcourse,"nasaldemons"—aphysicalimpossibi

发布:2022-10-16 标签:c++cundefined-behaviorlanguage-lawyer


为什么这些构造使用前增量和后增量未定义的行为?

问题描述#include<stdio.h>intmain(void){inti=0;i=i+++++i;printf("%d",i);//3i=1;i=(i++);printf("%d",i);//2Shouldbe1,no?volatileintu=0;u=u+++++u;pr

发布:2022-10-16 标签:cundefined-behaviorincrementoperator-precedencesequence-points


优化一个“while(1);&quot;在 C++0x 中

问题描述已更新,见下文!我听说并读到C++0x允许编译器为以下代码段打印Hello"IhaveheardandreadthatC++0xallowsancompilertoprint"Hello"forthefollowingsnippet#include<iostream>in

发布:2022-10-16 标签:loopsc++undefined-behaviorlanguage-lawyerc++11


为什么定义了无符号整数溢出行为但没有定义有符号整数溢出?

问题描述无符号整数溢出在C和C++标准中都有很好的定义.例如,C99标准(§6.2.5/9)状态UnsignedintegeroverflowiswelldefinedbyboththeCandC++standards.Forexample,theC99standard(§6.2

发布:2022-10-16 标签:c++cundefined-behaviorinteger-overflow


对于 C++14 中不确定值和未定义行为的使用,C++ 标准是否发生了变化?

问题描述AscoveredinDoesinitializationentaillvalue-to-rvalueconversion?Isintx=x;UB?theC++standardhasasurprisingexampleinsection3.3.2Pointofdeclar

发布:2022-10-16 标签:c++undefined-behaviorlanguage-lawyerc++11c++14


访问不活动的工会成员和未定义的行为?

问题描述IwasundertheimpressionthataccessingaunionmemberotherthanthelastonesetisUB,butIcan'tseemtofindasolidreference(otherthananswersclaimingit'

发布:2022-10-16 标签:c++undefined-behaviorlanguage-lawyerunions


使用 volatile 关键字修改 const 变量

问题描述我在回答问题并制作了这个测试程序.Iwasansweringaquestionandmadethistestprogram.#include<stdio.h>intmain(){volatileconstintv=5;int*a=&v;*a=4;printf("%d",v

发布:2022-10-16 标签:constantsvolatilecundefined-behavior


只要未实际修改,是否允许在 const 定义的对象上丢弃 const?

问题描述是否允许:constintconst_array[]={42};intmaybe_inc(boolwrite,int*array){if(write)array[0]++;returnarray[0];}intmain(){returnmaybe_inc(false,co

发布:2022-10-16 标签:constantsc++undefined-behaviorconst-cast


临时对象最初是 const 吗?

问题描述这个代码是UB吗?IsthiscodeUB?structA{voidnonconst(){}};constA&a=A{};const_cast<A&>(a).nonconst();换句话说,(临时)对象最初是const吗?我浏览了标准,但找不到答案,因此希望能引用相关部分

发布:2022-10-16 标签:constantsc++undefined-behaviorlanguage-lawyertemporary-objects