Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/158.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
这段代码怎么能像我看到的那样工作? 我有一个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; } unsigned int测试(std::vector actionQueue){ unsigned int theLastCount=actionQueue.size()-1; std::vector::const_reverse_迭代器rItr=actionQueue.rbegin(); std::vector::const_reverse_iterator rEndItr=actionQueue.rend(); 对于(;rItr!=rEndItr;++rItr,--最后计数){ const CAction&fileAction=*rItr; if(fileAction.test()){ 继续; } 返回最后一次计数; } assert(theLastCount==0);//这怎么会失败呢? 返回最后一次计数; }_C++_Puzzle_Fencepost - Fatal编程技术网

这段代码怎么能像我看到的那样工作? 我有一个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; } unsigned int测试(std::vector actionQueue){ unsigned int theLastCount=actionQueue.size()-1; std::vector::const_reverse_迭代器rItr=actionQueue.rbegin(); std::vector::const_reverse_iterator rEndItr=actionQueue.rend(); 对于(;rItr!=rEndItr;++rItr,--最后计数){ const CAction&fileAction=*rItr; if(fileAction.test()){ 继续; } 返回最后一次计数; } assert(theLastCount==0);//这怎么会失败呢? 返回最后一次计数; }

这段代码怎么能像我看到的那样工作? 我有一个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; } unsigned int测试(std::vector actionQueue){ unsigned int theLastCount=actionQueue.size()-1; std::vector::const_reverse_迭代器rItr=actionQueue.rbegin(); std::vector::const_reverse_iterator rEndItr=actionQueue.rend(); 对于(;rItr!=rEndItr;++rItr,--最后计数){ const CAction&fileAction=*rItr; if(fileAction.test()){ 继续; } 返回最后一次计数; } assert(theLastCount==0);//这怎么会失败呢? 返回最后一次计数; },c++,puzzle,fencepost,C++,Puzzle,Fencepost,不知何故,循环完成后最后的计数不是零 从我对逻辑的理解来看,这是不可能的,除非: 另一个线程端影响了actionQueue(我认为这是不可能的) 发生了一些暂时的内存损坏 我是否遗漏了一些愚蠢的东西,我的代码中是否有错误?请注意,在我看到的例子中,LastCount应该初始化为1,因为向量有两个元素。如果您的actionQueue为空,则 unsigned int theLastCount = actionQueue.size() - 1; 将最后计数设置为可能的最大无符号整数。内部循环将永远

不知何故,循环完成后最后的计数不是零

从我对逻辑的理解来看,这是不可能的,除非:

  • 另一个线程端影响了actionQueue(我认为这是不可能的)
  • 发生了一些暂时的内存损坏

  • 我是否遗漏了一些愚蠢的东西,我的代码中是否有错误?请注意,在我看到的例子中,LastCount应该初始化为1,因为向量有两个元素。

    如果您的actionQueue为空,则

    unsigned int theLastCount = actionQueue.size() - 1;
    
    将最后计数设置为可能的最大无符号整数。内部循环将永远不会执行,因为反向迭代器彼此相等(
    rbegin()==rend()
    在空容器上),因此您将使用
    lastCount
    来命中断言,它等于一个惊人的大数。

    我相信,如果所有文件操作都通过了test(),则lastCount将是-1。考虑:

    最后一次计数从actionQueue.size()开始-1。对于actionQueue中的每个项目,将其减量一次-也就是说,它现在是actionQueue.size()-1-actionQueue.size()=-1。想想看。LastCount保留当前迭代器的索引。但是当当前迭代器为rend时,则在数组的开头之前有一个迭代器,即-1


    编辑:哦,没有签名。但是,由于您只测试等于零,因此积分溢出在这里并不重要。

    如果队列为空怎么办

    最后的计数将是-1,然后…:-)

    这是因为如果所有元素的
    test()
    都通过,那么最后的计数应该变成-1。由于没有剩余的元素,并且如果存在元素,则最后一个计数始终是有效的元素索引。否则它应该变成-1


    注意:将最后计数的类型从
    无符号整数更改为
    int

    请编译并运行您的代码,然后再发布到此处!首先,这段代码不能编译(我不会告诉你为什么——你可以问你的编译器)。其次,您的断言永远不会成功,因为最后的计数将始终是(unsigned int)-1。

    好的一点,是真的,但是,在我的例子中,如果向量为空,函数将永远不会在第一个位置被调用。不过,更一般地说,如果循环运行到结束,您将始终拥有最大unsigned值。在
    actionQueue.size()-1
    处启动计数器,如果从未在循环中点击
    return
    语句,则可能会将其递减
    actionQueue.size()
    次。它怎么可能不失败?请参阅DeadMG的答案。@PigBen,如果内部返回被命中,则返回值未被测试。如果你不参加测试,你就不会失败。错误地为这篇文章创建代码的书面版本,实际代码返回无符号整数。@William:好的。那让我看看还有什么!代码是可以编译的,问题是这个帖子的简化。我现在需要弄清楚的问题是,为什么到达循环的结尾不可复制,其他地方出了一次问题,它发现了这个bug,这个bug是以前没有遇到过的,尽管之前有数千次调用。旧的一个bug发现另一个bug在代码中潜伏了很长时间。
    void test(std::vector<CAction> actionQueue) 
    {
      unsigned int theLastCount = actionQueue.size() - 1;
      /** Omitted code ***/
      {
        /** Omitted code ***/
        return theLastCount;
      }
      return theLastCount;
    }
    
    assert(theLastCount == -1);//correct assert!