不确定分段错误在哪里 当试图编译C++程序时,我会遇到分段错误的问题,但不确定问题的所在。我怀疑问题出在.find()上。。。。。是否是迭代器运算符

不确定分段错误在哪里 当试图编译C++程序时,我会遇到分段错误的问题,但不确定问题的所在。我怀疑问题出在.find()上。。。。。是否是迭代器运算符,c++,iterator,segmentation-fault,find,b-tree,C++,Iterator,Segmentation Fault,Find,B Tree,以下是test01.cpp的一部分,我在其中运行它来测试代码,并使用print语句找出问题所在: bool confirmEverythingMatches(const btree<long>& testContainer, const set<long>& stableContainer) { cout << "Confirms the btree and the set " "contain exactly the

以下是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; 
}
我发现当它进入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];
            }
        }
    }
}

好的,基于这个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;
}
if(tmp->uuuuu子体=nullptr){

std::cout这就是调试器存在的原因。在调试器中运行您的程序,让程序失败,然后调试器将告诉您出错的位置和原因

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


祝你好运!

这里的问题代码太多了,真的,你能试着把它删减到a吗?请你的问题提供a。包含的链接提供了如何进行的帮助。删减了你尝试了多少调试?打印出来没有多大帮助,但调试会向你显示程序和数据的状态,这些状态非常重要找出这个问题更有用。
0x000000000018
看起来您正试图在
btree::find
中使用空指针。使用调试器找出具体的位置。实现的哪一部分?我有btree.h、btree.tem和btree_迭代器.h和btree_迭代器.tem。btree是您的实现吗?我不确定它是什么重新创建tem文件。也许只需btree.h就足够了(尤其是find()实现)。这有帮助吗?子体[i]是否会导致segfault?
/**
 * 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
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];
    }
}