我一直认为:
char 用字节表示,sizeof (char) 总是 1, chars 计)是 RAM 的字节数(+交换空间)。但是现在我已经阅读了 Wikipedia entry on the byte我不再那么确定了。
我的哪些假设是错误的?哪一个是危险的?
最佳答案
是的,char和 byte几乎一样。一个字节是最小的可寻址内存量,char 也是如此。在 C. char总是大小为 1。
根据规范,3.6 字节部分:
byte
addressable unit of data storage large enough to hold any member of the basic character set of the execution environment
以及3.7.1 部分字符:
character
single-byte character
<C> bit representation that fits in a byte
A char有 CHAR_BIT位。它可以是任何数字(嗯,根据规范,8 或更大),但大多数情况下肯定是 8。有 16 位和 32 位的真实机器 char类型,虽然。 CHAR_BIT在 limits.h 中定义.
根据规范,5.2.4.2.1 整数类型的大小<limits.h> 部分:
The values given below shall be replaced by constant expressions suitable for use in
#ifpreprocessing directives. Moreover, except forCHAR_BITandMB_LEN_MAX, the following shall be replaced by expressions that have the same type as would an expression that is an object of the corresponding type converted according to the integer promotions. Their implementation-defined values shall be equal or greater in magnitude (absolute value) to those shown, with the same sign.— number of bits for smallest object that is not a bit-field (byte)
CHAR_BIT8
sizeof(char) == 1 .总是。
根据规范,6.5.3.4 部分 sizeof运算符,第 3 段:
When applied to an operand that has type
char,unsigned char, orsigned char, (or a qualified version thereof) the result is 1.
您可以分配尽可能多的内存,只要您的系统允许您分配 - 标准中没有任何内容定义可能是多少。例如,您可以想象一台具有云存储支持的内存分配系统的计算机 - 您的可分配内存实际上可能是无限的。
这是完整的规范部分 7.20.3.3 malloc功能:
Synopsis
1
#include <stdlib.h>
void *malloc(size_t size);Description
2 The
mallocfunction allocates space for an object whose size is specified bysizeand whose value is indeterminate.Returns
3 The
mallocfunction returns either a null pointer or a pointer to the allocated space.
这就是规范的全部内容,因此实际上没有任何可以依赖的限制。
关于c - `char` 总是总是有 8 位吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9727465/
我正在学习Rails,并阅读了关于乐观锁的内容。我已将类型为integer的lock_version列添加到我的articles表中。但现在每当我第一次尝试更新记录时,我都会收到StaleObjectError异常。这是我的迁移:classAddLockVersionToArticle当我尝试通过Rails控制台更新文章时:article=Article.first=>#我这样做:article.title="newtitle"article.save我明白了:(0.3ms)begintransaction(0.3ms)UPDATE"articles"SET"title"='dwdwd
最好用一个例子来解释:文件1.rb:deffooputs123end文件2.rb:classArequire'file1'endA.new.foo将给出错误“':调用了私有(private)方法'foo'”。我可以通过执行A.new.send("foo")来解决这个问题,但是有没有办法公开导入的方法?编辑:澄清一下,我没有混淆include和require。另外,我不能使用正常包含的原因(正如许多人正确指出的那样)是因为这是元编程设置的一部分。我需要允许用户在运行时添加功能;例如,他可以说“run-this-app--includefile1.rb”,应用程序的行为将根据他在file1
这个问题在这里已经有了答案:关闭11年前。PossibleDuplicate:RubyblockandunparenthesizedargumentsWhatisthedifferenceorvalueoftheseblockcodingstylesinRuby?我一直认为以下只是同一件事的两种表达方式:[1,2,3].collect{|i|i*2}[1,2,3].collectdo|i|i*2end但是我在我的一个ERB模板中发现了一些奇怪的行为,这两种语法似乎在做两件不同的事情。这段代码效果很好:m))}}%>但是当我将其重写为:m))endend%>...我最终得到了我的@men
抱歉,如果重复(我没找到)这只是为了确认Ruby的运算符==始终执行相等比较。IE。a==b将a的值与b的值进行比较,而不是像Java那样比较它们是否指向内存中的同一个对象(对于后者,在Ruby中,您应该使用a.object_id==b.object_id).因此,在Ruby中将字符串值与==进行比较是安全的(而在Java中这样做并不安全)谢谢编辑:问题在于任何Ruby对象的默认==行为,因为它会误导Java-C-C++程序员假设a==b比较引用本身,而不是引用内容。无论如何,你可以检查这段代码,使用字符串one="hello"two="he"two编辑2。所以,在Ruby中,比较a=
嘿,我有一个看起来像这样的测试test'createaccount'doifUser.create(email:'me@test.com',password:'blahblah')asserttrueelseassertUser.msgendend但是当我尝试运行它时,我收到如下错误消息:1)Error:UserTest#test_create_account:ActiveRecord::RecordNotUnique:PG::UniqueViolation:ERROR:duplicatekeyvalueviolatesuniqueconstraint"index_users_on_e
我正在尝试创建一条新路线,以便我可以利用RoR的路径变量功能,即new_game_path。就我而言,我想使用load_game_path我已经为适当的Controller创建了一个Action,目前路由如下:resources:gamesdoget'load',on::collectionend每次我使用load_games_path它都使用正确的URI,但似乎重定向到GamesController的显示操作并显示游戏的继承显示View。我检查了rakeroutes,我看到我新创建的路线似乎是所需的路径/games/load(文件路径:/views/games/load.html.e
如果我在我的rails项目中使用spork并且有一个像这样的spec_helper.rb文件require'spork'Spork.preforkdo...endSpork.each_rundo...end这是否意味着当我通过rspecspec运行我的规范时,我需要始终让spork运行?意思是,如果我还没有在终端窗口中执行$spork,是否意味着我的规范将无法正常运行? 最佳答案 没有。我们的spechelper中有spork,但我们不会经常使用它,因为它会减慢大型套件的整体测试速度。我们仅在快速迭代时运行spork,在TDD期间重
好吧,也许这很简单,但是......鉴于此:arr=("a".."z").to_aarr=>["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]..我正在尝试将所有“arr”值更改为“bad”为什么这行不通?arr.each{|v|v="bad"}arr=>["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v"
您可能自己知道,有时您必须通过Rails控制台在实时生产机器上执行任务...我通常这样开始:bundleexecrailsconsole-eproduction但由于它是生产机器,我想将Rails控制台的所有输入和输出记录到一个文件中,例如到/home/sshuser/myproject/console_sessions/2016_09_09__14_33_33.txt有人知道怎么做吗?我想自动启动记录器,但前提是我运行控制台?(我正在运行Rails3.2)谢谢! 最佳答案 这是一个解决方案,整个系统只有一个文件,不修改Rails项
我正在使用rubyversion1.9.3并且在启动thinserver时出现错误/usr/local/lib/ruby/gems/1.9.1/gems/bundler-1.12.4/lib/bundler/runtime.rb:100:in`require':/usr/local/lib/ruby/gems/1.9.1/gems/pdf-writer-1.1.8/lib/pdf/writer.rb:712:invalidmultibytechar(US-ASCII)(SyntaxError)/usr/local/lib/ruby/gems/1.9.1/gems/pdf-writer-