【问题标题】:Segmentation fault when assigning the return vector of a function to another vector将函数的返回向量分配给另一个向量时出现分段错误
【发布时间】:2019-07-31 03:26:53
【问题描述】:

我的一项家庭作业有问题,我们需要使用哈希表检测字符串向量中的重复字符串。我的代码可以正常构建和编译,但是当我尝试将重复检测算法的返回向量分配给重复向量时出现分段错误。我试图弄清楚为什么会发生这种情况,但找不到解决方案。我在下面附上了我的代码。

使用哈希表查找重复的功能##

 std::vector<std::string>find_duplicates_with_hashtable(std::vector<std::string> & strings) {

        std::vector<std::string> dups;
        typedef std::unordered_map<std::string, std::string> hashtable;
        hashtable table;

        for (std::vector<std::string>::iterator i = strings.begin(); i < strings.end(); i++) {
        std::unordered_map<std::string, std::string>::const_iterator it = table.find(*i);
        if (it != table.end() && (std::find(dups.begin(), dups.end(), *i)) == dups.end()) {
            dups = find_duplicates_with_sorting(dups); // line causing the problem
        }
        table.emplace(*i, *i);
        }

        return dups; 
  }

用于检查给定向量中的任何元素是否存在于重复向量中的函数

std::vector<std::string> find_duplicates_with_sorting(std::vector<std::string> & strings) {
    std::vector<std::string> dups;

    std::sort(strings.begin(), strings.end());

    for( unsigned int i = 0; i < strings.size() - 1; ++i ) {
        if( strings[i].compare(strings[i+1]) == 0 ) {
            std::string found_dup = strings[i];
            if( dups.size() == 0 ) {
                dups.push_back(found_dup);
            }
            else
            {
                std::string last_found_dup = dups[ dups.size() - 1 ];
                if( last_found_dup.compare(found_dup) != 0 ) {              // Not a dup of a dup
                    dups.push_back(found_dup);
                }
            }
        }
    }

    return dups;
}

这是调用哈希表函数的上下文

TEST(BaseHash, SuperShortVector)
{
    std::vector<std::string> dups_found;
    auto & search_vector      = super_short_vector;
    auto & known_dups_vector  = super_short_vector_dups;

    dups_found = find_duplicates_with_hashtable(search_vector);

    std::sort(dups_found.begin(), dups_found.end());
    std::sort(known_dups_vector.begin(), known_dups_vector.end());


}

导致问题的行由“find_duplicates_with_hashtable”函数中的注释标记

另外,由于这是一项家庭作业,如果有人能解释我做错了什么并给我一个大致的方向来解决问题,我将不胜感激,因为我只是复制粘贴代码不会帮助我学习

对不起,如果代码很糟糕。我无法理解如何使用哈希表。

谢谢你:)

【问题讨论】:

  • 您学会如何使用gdb 或类似工具了吗?那将非常有帮助。仅通过查看您不熟悉的代码来查找段错误的来源通常非常耗时
  • @JeffreyCash 我尝试使用 ddd 但我对它非常陌生,所以我不明白为什么会发生段错误。我通过注释掉不同的行并查看哪一行破坏了代码来隔离导致问题的行。

标签: c++ vector hashtable


【解决方案1】:

段错误发生在这里:

for( unsigned int i = 0; i < strings.size() - 1; ++i ) {
        if( strings[i].compare(strings[i+1]) == 0 ) {

问题是您将一个无符号值i 与从strings.size() - 1 返回的无符号值进行比较。当strings.size()0 时,这部分i &lt; strings.size() - 1 将检查i 是否小于最大整数值,这将(基本上)始终为真。

strings 的长度为0 或1 时,这会导致strings[i+1] 出现段错误。

这可以通过多种方式解决,但for( int i = 0; i &lt; (int)strings.size() - 1; ++i ) { 将是一种快速而肮脏的解决方法。

【讨论】:

  • 我试过了,但它仍然会导致段错误。我觉得问题应该只出在 find_duplicates_with_hashtable 函数上,因为其余的代码已经由教授提供了。
  • 我在使用 unordered_map 和 vector 时会不会有问题?我遵循了 c++ 标准库文档是如何使用它们的。
  • 嗯,它为我编译和运行。它只是没有找到任何重复,我认为这可能与总是将空向量传递给dups = find_duplicates_with_sorting(dups)
  • 根据super_short_vector 的内容有可能存在另一个段错误,但此答案中的修复为我解决了一个段错误
  • 哇哦。我修好了它。显然,您关于传递空向量的建议解决了这个问题。我只需要将其更改为 'dups = find_duplicates_with_sorting(strings)' 。我想这只是我的粗心。谢谢你的帮助:)
猜你喜欢
  • 1970-01-01
  • 2017-10-20
  • 2018-02-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-29
  • 2016-02-15
  • 2021-08-05
相关资源
最近更新 更多