这段代码怎么会像我看到的那样表现呢?

发布于 2024-10-12 08:19:17 字数 846 浏览 9 评论 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;
}

不知何故,循环完成后 theLastCount 不为零。

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

  1. 其他一些线程影响了actionQueue(我认为这是不可能的)。
  2. 发生了一些暂时性内存损坏。

我在这里错过了一些愚蠢的事情吗?我的代码中是否显示了错误?请注意,在我看到此情况的情况下,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:

  1. Some other thread side effected the actionQueue (which I don't think is possible).
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

小镇女孩 2024-10-19 08:19:17

我相信,如果所有 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.

樱花坊 2024-10-19 08:19:17

如果您的 actionQueue 为空,则将

unsigned int theLastCount = actionQueue.size() - 1;

theLastCount 设置为可能的最大无符号整数。内部循环永远不会执行,因为反向迭代器彼此相等(空容器上的 rbegin() == rend()),因此您将使用 theLastCount< 来命中断言/code> 等于某个大得惊人的数字。

If your actionQueue is empty, then

unsigned int theLastCount = actionQueue.size() - 1;

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 with theLastCount equal to some staggeringly huge number.

超可爱的懒熊 2024-10-19 08:19:17

请先编译并运行您的代码,然后再将其发布到此处!首先,这段代码无法编译(我不会告诉你原因——你可以询问你的编译器)。其次,您的断言永远不会成功,因为 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.

念﹏祤嫣 2024-10-19 08:19:17
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。由于没有剩余元素,并且如果存在元素,则 LastCount 始终是有效的元素索引。否则它应该变成-1。

注意:theLastCount的类型从unsigned int更改为int

void test(std::vector<CAction> actionQueue) 
{
  unsigned int theLastCount = actionQueue.size() - 1;
  /** Omitted code ***/
  {
    /** Omitted code ***/
    return theLastCount;
  }
  return theLastCount;
}

Forget about the error that you're unable to reproduce. But here is one serious problem. The return type is void, yet you return unsigned int!! How come?


I think, you need to write this:

assert(theLastCount == -1);//correct assert!

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 from unsigned int to int.

阳光下的泡沫是彩色的 2024-10-19 08:19:17

如果队列为空怎么办?

theLastCount 将是 -1,然后...:-)

What if the queue is empty?

theLastCount will be -1, and then... :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文