我有一个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;
}
不知何故,循环完成后,LastCount不为零。
从我对逻辑的解读来看,这应该是不可能的,除非:
我在这里错过了一些愚蠢的东西,我的代码中是否有错误显示?请注意,在我看到这个的情况下,由于向量有两个元素,因此应该将LastCount初始化为1。
答案 0 :(得分:5)
我相信如果test()传递给所有fileActions,则theLastCount将为-1。考虑:
theLastCount从actionQueue.size()开始。你在actionQueue中为每个项目递减一次 - 也就是说,它现在是actionQueue.size() - 1 - actionQueue.size()= -1。想一想。 theLastCount保留当前迭代器的索引。但是当当前的迭代器是rend时,那就是在数组开始之前的一个迭代器 - 这是-1。
编辑:哦,它没有签名。但是因为你只测试等于零,所以积分溢出在这里并不重要。
答案 1 :(得分:2)
如果你的actionQueue为空,那么
unsigned int theLastCount = actionQueue.size() - 1;
将theLastCount
设置为最大可能的无符号整数。内部循环将永远不会执行,因为反向迭代器彼此相等(空容器上的rbegin() == rend()
),因此您将使用theLastCount
等于某个数字庞大的数字来执行断言。
答案 2 :(得分:1)
void test(std::vector<CAction> actionQueue)
{
unsigned int theLastCount = actionQueue.size() - 1;
/** Omitted code ***/
{
/** Omitted code ***/
return theLastCount;
}
return theLastCount;
}
忘掉你无法重现的错误。但这是一个严重的问题。返回类型为void
,但您返回unsigned int
!!怎么样?
我想,你需要写下这个:
assert(theLastCount == -1);//correct assert!
这是因为如果test()
传递所有元素,那么theLastCount应该变为-1。由于没有剩余元素,并且theLastCount始终是有效元素索引如果有元素。否则它应该变为-1。
注意:将theLastCount
的类型从unsigned int
更改为int
。
答案 3 :(得分:1)
请在此处发布之前编译并运行您的代码!首先,这段代码没有编译(我没告诉你为什么 - 你可以问你的编译器)。其次,你的断言永远不会成功,因为theLastCount总是(unsigned int)-1。
答案 4 :(得分:0)
如果队列为空怎么办?
theLastCount
将为-1,然后......: - )