C 和 C++ 标准都允许相同整数类型的有符号和无符号变体相互使用别名。例如,unsigned int* 和 int* 可能是别名。但这还不是全部,因为它们显然具有不同范围的可表示值。我有以下假设:
int*读取一个unsigned int,该值必须在int范围内,否则会发生整数溢出并且行为未定义。这是正确的吗?unsigned int* 读取 int,则负值环绕,就好像它们被强制转换为 unsigned int 一样。这是正确的吗?int 和 unsigned int 范围内,则通过任一类型的指针访问它都是完全定义的并给出相同的值。这是正确的吗?此外,兼容但不等价的整数类型呢?
int 和 long 具有相同范围、对齐方式等的系统上,int* 和 long* 别名? (我想不会。)char16_t* 和uint_least16_t* 可以别名吗?我怀疑这在 C 和 C++ 之间是不同的。在 C 语言中,char16_t 是 uint_least16_t 的类型定义(对吗?)。在 C++ 中,char16_t 是它自己的原始类型,与 uint_least16_t 兼容。与 C 不同,C++ 似乎也不异常(exception)地允许兼容但不同的类型作为别名。最佳答案
If an
unsigned intis read through anint*, the value must be within the range ofintor an integer overflow occurs and the behaviour is undefined. Is this correct?
为什么它会是未定义的?没有整数溢出,因为没有完成转换或计算。我们采用 unsigned int 的对象表示对象并通过 int 查看它. unsigned int的值是通过什么方式得到的对象转置为 int 的值完全由实现定义。
If an
intis read through anunsigned int*, negative values wrap around as if they were casted to unsigned int. Is this correct?
取决于表示。使用二进制补码和等效填充,是的。虽然没有符号幅度 - 来自 int 的类型转换至 unsigned总是通过同余定义:
If the destination type is
unsigned, the resulting value is the least unsigned integer congruent to the source integer (modulo 2n wherenis the number of bits used to represent the unsigned type). [ Note: In a two’s complement representation, this conversion is conceptual and there is no change in the bit pattern (if there is no truncation). — end note ]
现在考虑
10000000 00000001 // -1 in signed magnitude for 16-bit int
如果解释为 unsigned,这肯定是 215+1 .不过,转换会产生 216-1。
If the value is within the range of both int and unsigned int, accessing it through a pointer of either type is fully defined and gives the same value. Is this correct?
同样,使用二进制补码和等效填充,是的。有符号的大小我们可能有 -0 .
On systems where
intandlonghave the same range, alignment, etc., canint*andlong*alias? (I assume not.)
没有。它们是独立的类型。
Can
char16_t*anduint_least16_t*alias?
技术上不是,但这似乎是对标准的不必要限制。
Types
char16_tandchar32_tdenote distinct types with the same size, signedness, and alignment asuint_least16_tanduint_least32_t, respectively, in<cstdint>, called the underlying types.
所以它应该是几乎没有任何风险的(因为不应该有任何填充)。
关于c++ - 其他等效的有符号和无符号类型的别名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27109701/
我试图在一个项目中使用rake,如果我把所有东西都放到Rakefile中,它会很大并且很难读取/找到东西,所以我试着将每个命名空间放在lib/rake中它自己的文件中,我添加了这个到我的rake文件的顶部:Dir['#{File.dirname(__FILE__)}/lib/rake/*.rake'].map{|f|requiref}它加载文件没问题,但没有任务。我现在只有一个.rake文件作为测试,名为“servers.rake”,它看起来像这样:namespace:serverdotask:testdoputs"test"endend所以当我运行rakeserver:testid时
我的目标是转换表单输入,例如“100兆字节”或“1GB”,并将其转换为我可以存储在数据库中的文件大小(以千字节为单位)。目前,我有这个:defquota_convert@regex=/([0-9]+)(.*)s/@sizes=%w{kilobytemegabytegigabyte}m=self.quota.match(@regex)if@sizes.include?m[2]eval("self.quota=#{m[1]}.#{m[2]}")endend这有效,但前提是输入是倍数(“gigabytes”,而不是“gigabyte”)并且由于使用了eval看起来疯狂不安全。所以,功能正常,
我正在寻找执行以下操作的正确语法(在Perl、Shell或Ruby中):#variabletoaccessthedatalinesappendedasafileEND_OF_SCRIPT_MARKERrawdatastartshereanditcontinues. 最佳答案 Perl用__DATA__做这个:#!/usr/bin/perlusestrict;usewarnings;while(){print;}__DATA__Texttoprintgoeshere 关于ruby-如何将脚
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
我可以得到Infinity和NaNn=9.0/0#=>Infinityn.class#=>Floatm=0/0.0#=>NaNm.class#=>Float但是当我想直接访问Infinity或NaN时:Infinity#=>uninitializedconstantInfinity(NameError)NaN#=>uninitializedconstantNaN(NameError)什么是Infinity和NaN?它们是对象、关键字还是其他东西? 最佳答案 您看到打印为Infinity和NaN的只是Float类的两个特殊实例的字符串
我不确定传递给方法的对象的类型是否正确。我可能会将一个字符串传递给一个只能处理整数的函数。某种运行时保证怎么样?我看不到比以下更好的选择:defsomeFixNumMangler(input)raise"wrongtype:integerrequired"unlessinput.class==FixNumother_stuffend有更好的选择吗? 最佳答案 使用Kernel#Integer在使用之前转换输入的方法。当无法以任何合理的方式将输入转换为整数时,它将引发ArgumentError。defmy_method(number)
有时我需要处理键/值数据。我不喜欢使用数组,因为它们在大小上没有限制(很容易不小心添加超过2个项目,而且您最终需要稍后验证大小)。此外,0和1的索引变成了魔数(MagicNumber),并且在传达含义方面做得很差(“当我说0时,我的意思是head...”)。散列也不合适,因为可能会不小心添加额外的条目。我写了下面的类来解决这个问题:classPairattr_accessor:head,:taildefinitialize(h,t)@head,@tail=h,tendend它工作得很好并且解决了问题,但我很想知道:Ruby标准库是否已经带有这样一个类? 最佳
我正在尝试解析一个CSV文件并使用SQL命令自动为其创建一个表。CSV中的第一行给出了列标题。但我需要推断每个列的类型。Ruby中是否有任何函数可以找到每个字段中内容的类型。例如,CSV行:"12012","Test","1233.22","12:21:22","10/10/2009"应该产生像这样的类型['integer','string','float','time','date']谢谢! 最佳答案 require'time'defto_something(str)if(num=Integer(str)rescueFloat(s
我正在玩HTML5视频并且在ERB中有以下片段:mp4视频从在我的开发环境中运行的服务器很好地流式传输到chrome。然而firefox显示带有海报图像的视频播放器,但带有一个大X。问题似乎是mongrel不确定ogv扩展的mime类型,并且只返回text/plain,如curl所示:$curl-Ihttp://0.0.0.0:3000/pr6.ogvHTTP/1.1200OKConnection:closeDate:Mon,19Apr201012:33:50GMTLast-Modified:Sun,18Apr201012:46:07GMTContent-Type:text/plain
我需要一些关于TDD概念的帮助。假设我有以下代码defexecute(command)casecommandwhen"c"create_new_characterwhen"i"display_inventoryendenddefcreate_new_character#dostufftocreatenewcharacterenddefdisplay_inventory#dostufftodisplayinventoryend现在我不确定要为什么编写单元测试。如果我为execute方法编写单元测试,那不是几乎涵盖了我对create_new_character和display_invent