不确定分割故障在哪里

Not sure where the segmentation fault is

本文关键字:在哪里 故障 分割 不确定      更新时间:2023-10-16

在尝试编译c++程序时,我遇到了分段错误问题,但不确定问题所在。我怀疑问题出在.find() .....上可能是迭代器操作符<和>

下面是test01.cpp的一部分,我在其中运行它来测试代码并使用print语句来找出问题所在:

bool confirmEverythingMatches(const btree<long>& testContainer, const set<long>& stableContainer) {
  cout << "Confirms the btree and the set " 
          "contain exactly the same values..." << endl;

  for (long i = kMinInteger; i <= kMaxInteger; i++) {
    cout << "Start of for-loop to find iterator for comparisons..." << endl;
    if (stableContainer.find(i) != stableContainer.end()) {
      cout << "can find i (" << i << ") in stableContainer!" << endl;
    } else {
      cout << "cannot find i (" << i << ") in stableContainer!" << endl;
    }
    cout << "In between finding i in stable and testContainers..." << endl;
    if (testContainer.find(i) != testContainer.end()) {
      cout << "can find i (" << i << ") in testContainer!" << endl;
    } else {
      cout << "cannot find i (" << i << ") in testContainer!" << endl;
    }
    cout << "Before assigning the find to boolean variables..." << endl;
    bool foundInTree = (testContainer.find(i) != testContainer.end());
    cout << "testContainer.find(i) != testContainer.end()" << endl;

    bool foundInSet = (stableContainer.find(i) != stableContainer.end());
    cout << "stableContainer.find(i) != stableContainer.end()" << endl;
    if (foundInTree != foundInSet) {
      cout << "- btree and set don't contain the same data!" << endl; 
      cout << "Mismatch at element: " << i << endl;
      return false;
    } else {cout << "foundInTree == foundInSet!!!" << i << endl;}
  }
  cout << "- btree checks out just fine." << endl;
  return true;
}
}  // namespace close

/**
 * Codes for testing various bits and pieces. Most of the code is commented out
 * you should uncomment it as appropriate.
 **/
int main(void) {
  // initialise random number generator with 'random' seed
  initRandom();
  cout << "after initRandom().." << endl;
  // insert lots of random numbers and compare with a known correct container
  btree<long> testContainer(99);
  cout << "after specifying max node elements in testContainer.." << endl;
  set<long> stableContainer;
  cout << "after constructing stableContainer.." << endl;
  insertRandomNumbers(testContainer, stableContainer, 1000000);
  cout << "after inserting random numbers into testContainer and for success inserts, also into stableContainer.." << endl;
  btree<long> btcpy = testContainer;
  cout << "after copy assigning a copy of testContainer to btcopy.." << endl;
  confirmEverythingMatches(btcpy, stableContainer);
  cout << "after confirming everything internally matches between testContainer and stableContainer.." << endl;
  return 0; 
}

运行程序时得到的输出(编译时没有问题)是这样的:

Confirms the btree and the set contain exactly the same values...
Start of for-loop to find iterator for comparisons...
cannot find i (1000000) in stableContainer!
In between finding i in stable and testContainers...
ASAN:DEADLYSIGNAL
=================================================================
==7345==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000018 (pc 0x000108d132a8 bp 0x000000000000 sp 0x7fff56eee6f0 T0)
    #0 0x108d132a7 in btree<long>::find(long const&) const (test01+0x1000022a7)
AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV (test01+0x1000022a7) in btree<long>::find(long const&) const
==7345==ABORTING
Abort trap: 6

当我试图在另一台机器上运行它时,我也得到了这个错误:

==29936==ERROR: AddressSanitizer failed to allocate 0x200000 (2097152) bytes of SizeClassAllocator32: 12

我发现当它进入find()时,它会有分段错误:

/**
 * Identical in functionality to the non-const version of find,
 * save the fact that what's pointed to by the returned iterator
 * is deemed as const and immutable.
 *
 * @param elem the client element we are trying to match.
 * @return an iterator to the matching element, or whatever the
 *         const end() returns if no such match was ever found.
 */
template<typename T> typename btree<T>::const_iterator
btree<T>::find(const T& elem) const {
    std::cout << "CONST ITERATOR'S FIND" << std::endl;
Node *tmp_ = root_;
std::cout << "1" << std::endl;
while(true) {
std::cout << "2" << std::endl;
size_t i;
std::cout << "3" << std::endl;
// go through all elements from root to tail
for (i = 0; i < tmp_->__occupied_size_; ++i) {
    std::cout << "4" << std::endl;
    if (tmp_->__elem_[i] == elem) {
        std::cout << "5" << std::endl;
        // find the elem, return an iterator
        return const_iterator(tmp_, i, this);
        std::cout << "6" << std::endl;
    } else if (tmp_->__elem_[i] > elem) {
        std::cout << "7" << std::endl;
        // elem is not in current Node, go to descendants
        // for the elem.
        if (tmp_->__descendants_ == nullptr) {
            std::cout << "8" << std::endl;
            return cend();
            std::cout << "9" << std::endl;
        } else {
            std::cout << "10" << std::endl;
            tmp_ = tmp_->__descendants_[i];
            std::cout << "11" << std::endl;
            break;
                }
            }
        }
        // handling boundaries cases
        if (i == tmp_->__occupied_size_) {
            std::cout << "12" << std::endl;
            if (tmp_->__descendants_[i] == nullptr) {
                std::cout << "13" << std::endl;
                return cend();
                std::cout << "14" << std::endl;
            } else {
                std::cout << "15" << std::endl;
                tmp_ = tmp_->__descendants_[i];
            }
        }
    }
}

查找的打印语句为:

CONST ITERATOR'S FIND
1
2
3
4
4
7
10
11
2
3
4
7
10
11
ASAN:DEADLYSIGNAL

好的,那么根据这个find函数的实现,我认为问题可能位于

if (tmp_->__descendants_ == nullptr) {
    std::cout << "8" << std::endl;
    return cend();
    std::cout << "9" << std::endl;
} else {
    std::cout << "10" << std::endl;
    tmp_ = tmp_->__descendants_[i];
    std::cout << "11" << std::endl;
    break;
}

然后

// handling boundaries cases
if (i == tmp_->__occupied_size_) {
    std::cout << "12" << std::endl;
    if (tmp_->__descendants_[i] == nullptr) {
        std::cout << "13" << std::endl;
        return cend();
        std::cout << "14" << std::endl;
    } else {
        std::cout << "15" << std::endl;
        tmp_ = tmp_->__descendants_[i];
    }
}

所以,你正在检查tmp->__descendants_是否为空。如果不是,则设置tmp_ = tmp_->descendants_[i];

注意:你只是检查__descendants_指针是否为空,你没有检查__descendants_ [i]是否为空!

如果tmp->__descendants_[i]为空(或从后代数组中取出)怎么办?

如果该值为null,则tmp_->occupied_size_可能会导致段错误。

注2:由于某些原因,您使用相同的索引"i"来遍历__elem_和__descendants_。我不确定,后代是如何创建的,但这可能也是一个问题。

这就是调试器存在的原因。在调试器中运行你的程序,让程序失败,然后调试器会告诉你哪里出错了,为什么出错了。

看起来你可能有很多代码要浏览,这里没有人真的想这样做,因为这不是一个简洁的问题。

祝你好运!