这段代码怎么会像我看到的那样表现呢?
我有一个 C++ 应用程序,它发生了一次断言失败,我无法重现。下面是失败过一次的代码:
unsigned int test(std::vector<CAction> actionQueue) {
unsigned int theLastCount = actionQueue.size() - 1;
std::vector<CAction>::const_reverse_iterator rItr = actionQueue.rbegin();
std::vector<CAction>::const_reverse_iterator rEndItr = actionQueue.rend();
for (; rItr != rEndItr; ++rItr, --theLastCount) {
const CAction &fileAction = *rItr;
if (fileAction.test()) {
continue;
}
return theLastCount;
}
assert(theLastCount == 0); // How could this fail?
return theLastCount;
}
不知何故,循环完成后 theLastCount 不为零。
从我对逻辑的理解来看,这应该是不可能的,除非:
- 其他一些线程影响了actionQueue(我认为这是不可能的)。
- 发生了一些暂时性内存损坏。
我在这里错过了一些愚蠢的事情吗?我的代码中是否显示了错误?请注意,在我看到此情况的情况下,theLastCount 应该已初始化为 1,因为向量有两个元素。
I have a C++ application that had a one time assertion failure that I cannot reproduce. Here is the code that failed once:
unsigned int test(std::vector<CAction> actionQueue) {
unsigned int theLastCount = actionQueue.size() - 1;
std::vector<CAction>::const_reverse_iterator rItr = actionQueue.rbegin();
std::vector<CAction>::const_reverse_iterator rEndItr = actionQueue.rend();
for (; rItr != rEndItr; ++rItr, --theLastCount) {
const CAction &fileAction = *rItr;
if (fileAction.test()) {
continue;
}
return theLastCount;
}
assert(theLastCount == 0); // How could this fail?
return theLastCount;
}
Somehow, theLastCount was not zero after the loop completed.
From my reading of the logic, this should be impossible unless:
- Some other thread side effected the actionQueue (which I don't think is possible).
- Some transient memory corruption occurred.
Am I missing something stupid here, is there a bug in my code shown? Note that in the occurrence where I saw this, theLastCount should have been initialized to one as the vector had two elements.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我相信,如果所有 fileActions 都通过了 test(),那么 theLastCount 将为 -1。考虑:
theLastCount 从actionQueue.size() -1 开始。您可以为actionQueue 中的每个项目将其递减一次,即现在为actionQueue.size() - 1 - actionQueue.size() = -1。想一想。 theLastCount 保留当前迭代器的索引。但是,当当前迭代器为 rend 时,则该迭代器是数组开头之前的一个迭代器,即 -1。
编辑:哦,它没有签名。但由于您只测试是否等于零,因此积分溢出在这里并不重要。
I believe that if test() passed for all fileActions, theLastCount would be -1. Consider:
theLastCount starts at actionQueue.size() -1. You decrement it once for each item in actionQueue- that is, it is now actionQueue.size() - 1 - actionQueue.size() = -1. Think about it. theLastCount keeps the index of the current iterator. But when the current iterator is rend, then that is one iterator before the beginning of the array- which is -1.
Edit: Oh, it's unsigned. But since you only test for equality to zero, then the integral overflow doesn't matter an awful lot here.
如果您的 actionQueue 为空,则将
theLastCount
设置为可能的最大无符号整数。内部循环永远不会执行,因为反向迭代器彼此相等(空容器上的 rbegin() == rend()),因此您将使用theLastCount< 来命中断言/code> 等于某个大得惊人的数字。
If your actionQueue is empty, then
will set
theLastCount
to the maximum possible unsigned integer. The inner loop will never execute because the reverse iterators are equal to one another (rbegin() == rend()
on empty containers), and so you'll hit the assertion withtheLastCount
equal to some staggeringly huge number.请先编译并运行您的代码,然后再将其发布到此处!首先,这段代码无法编译(我不会告诉你原因——你可以询问你的编译器)。其次,您的断言永远不会成功,因为 theLastCount 将始终为 (unsigned int)-1。
Please compile and run your code before you post it here! Firstly, this code doesn't compile (I'm not telling you why -- you can ask your compiler). Secondly, your assertion can never have succeeded, because theLastCount will always be (unsigned int)-1.
忘记您无法重现的错误。但这里有一个严重的问题。返回类型是
void
,但你却返回unsigned int
!怎么会?我认为,你需要这样写:
那是因为如果
test()
通过了所有元素,那么 theLastCount 应该变成 -1。由于没有剩余元素,并且如果存在元素,则 LastCount 始终是有效的元素索引。否则它应该变成-1。注意:将
theLastCount
的类型从unsigned int
更改为int
。Forget about the error that you're unable to reproduce. But here is one serious problem. The return type is
void
, yet you returnunsigned int
!! How come?I think, you need to write this:
That is because if the
test()
pass for all elements, then theLastCount should become -1. Since there is no element left, and theLastCount always is valid element index if there is element. Else it should become -1.NOTE : Change the type of
theLastCount
fromunsigned int
toint
.如果队列为空怎么办?
theLastCount
将是 -1,然后...:-)What if the queue is empty?
theLastCount
will be -1, and then... :-)