我正在阅读this paper关于未定义的行为,示例“优化”之一看起来非常可疑:
if (arg2 == 0) ereport(ERROR, (errcode(ERRCODE_DIVISION_BY_ZERO), errmsg("division by zero"))); /* No overflow is possible */ PG_RETURN_INT32((int32) arg1 / arg2);Figure 2: An unexpected optimization voids the division-by-zero check, in
src/backend/utils/adt/int8.cof PostgreSQL. The call toereport(ERROR, :::)will raise an exception.
ereport将返回,并且删除了arg2 == 0检查,因为除法的存在意味着分母为非零,即arg2 != 0。ereport ,其描述如下: 84 /*----------
85 * New-style error reporting API: to be used in this way:
86 * ereport(ERROR,
87 * (errcode(ERRCODE_UNDEFINED_CURSOR),
88 * errmsg("portal \"%s\" not found", stmt->portalname),
89 * ... other errxxx() fields as needed ...));
90 *
91 * The error level is required, and so is a primary error message (errmsg
92 * or errmsg_internal). All else is optional. errcode() defaults to
93 * ERRCODE_INTERNAL_ERROR if elevel is ERROR or more, ERRCODE_WARNING
94 * if elevel is WARNING, or ERRCODE_SUCCESSFUL_COMPLETION if elevel is
95 * NOTICE or below.
96 *
97 * ereport_domain() allows a message domain to be specified, for modules that
98 * wish to use a different message catalog from the backend's. To avoid having
99 * one copy of the default text domain per .o file, we define it as NULL here
100 * and have errstart insert the default text domain. Modules can either use
101 * ereport_domain() directly, or preferably they can override the TEXTDOMAIN
102 * macro.
103 *
104 * If elevel >= ERROR, the call will not return; we try to inform the compiler
105 * of that via pg_unreachable(). However, no useful optimization effect is
106 * obtained unless the compiler sees elevel as a compile-time constant, else
107 * we're just adding code bloat. So, if __builtin_constant_p is available,
108 * use that to cause the second if() to vanish completely for non-constant
109 * cases. We avoid using a local variable because it's not necessary and
110 * prevents gcc from making the unreachability deduction at optlevel -O0.
111 *----------
最佳答案
Is the compiler free to assume that a function will always return?
ereport返回(例如,通过内联并检查代码)。ereport至少取决于一个#define和传入的值,因此我不确定,但是它的确设计为有条件地不返回(而且它调用了外部函数errstart,据编译器所知,可能会或可能不会返回)。因此,如果编译器确实假设它总是返回,那么要么编译器错误,要么ereport的实现错误,或者我完全误解了它。However, the programmer failed to inform the compiler that the call to ereport(ERROR, ::: ) does not return.
abort()或exit()或longjmp()。在C++中,它们也可以引发异常。并且需要允许他们有条件地执行此操作-属性noreturn表示该函数从不返回,而不是该函数可能不返回,并且缺少它就不能证明该函数是否返回。我对这两个标准的经验是,它们并不荒谬。ereport不返回,则“优化”将更改程序的可观察行为(从ereport所做的而不是返回的操作变为由于被零除而具有未定义的行为)。因此,这是禁止的。ereport不返回,则PostGreSQL问题与拒绝的GCC错误报告不同。The gcc guys are full of it. The issue that is relevant here is the C standard's definition of sequence points, and in particular the requirement that visible side effects of a later statement cannot happen before the execution of an earlier function call. The last time I pestered them about this, I got some lame claim that a SIGFPE wasn't a side effect within the definitions of the spec. At that point useful discussion stopped, because it's impossible to negotiate with someone who's willing to claim that.
-fwrapv选项与GCC进行比较,该选项将整数溢出从UB(标准所说)更改为环绕式(只有指定了选项,编译器才能保证)。在MIPS上,gcc有一个选项-mcheck-zero-division,它看起来确实定义了零除行为,但我从未使用过。We found seven similar issues in PostgreSQL, which were noted as “GCC bugs” in source code comments
关于c++ - 是否假定C/C++中的所有函数都返回?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20059532/
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
我试图在一个项目中使用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时
作为我的Rails应用程序的一部分,我编写了一个小导入程序,它从我们的LDAP系统中吸取数据并将其塞入一个用户表中。不幸的是,与LDAP相关的代码在遍历我们的32K用户时泄漏了大量内存,我一直无法弄清楚如何解决这个问题。这个问题似乎在某种程度上与LDAP库有关,因为当我删除对LDAP内容的调用时,内存使用情况会很好地稳定下来。此外,不断增加的对象是Net::BER::BerIdentifiedString和Net::BER::BerIdentifiedArray,它们都是LDAP库的一部分。当我运行导入时,内存使用量最终达到超过1GB的峰值。如果问题存在,我需要找到一些方法来更正我的代
Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题
给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru
我试图获取一个长度在1到10之间的字符串,并输出将字符串分解为大小为1、2或3的连续子字符串的所有可能方式。例如:输入:123456将整数分割成单个字符,然后继续查找组合。该代码将返回以下所有数组。[1,2,3,4,5,6][12,3,4,5,6][1,23,4,5,6][1,2,34,5,6][1,2,3,45,6][1,2,3,4,56][12,34,5,6][12,3,45,6][12,3,4,56][1,23,45,6][1,2,34,56][1,23,4,56][12,34,56][123,4,5,6][1,234,5,6][1,2,345,6][1,2,3,456][123
我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何
为什么4.1%2返回0.0999999999999996?但是4.2%2==0.2。 最佳答案 参见此处:WhatEveryProgrammerShouldKnowAboutFloating-PointArithmetic实数是无限的。计算机使用的位数有限(今天是32位、64位)。因此计算机进行的浮点运算不能代表所有的实数。0.1是这些数字之一。请注意,这不是与Ruby相关的问题,而是与所有编程语言相关的问题,因为它来自计算机表示实数的方式。 关于ruby-为什么4.1%2使用Ruby返
我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server