【问题标题】:c++ string assigned to const char* causes address out of bounds at the n-th iteration分配给 const char* 的 c++ 字符串导致第 n 次迭代时地址越界
【发布时间】:2014-08-01 10:40:24
【问题描述】:

我遇到了“地址 0x4811 越界”的问题,我怀疑这是由传递给 const char*std::string 引起的。奇怪的是,这只有在数千次迭代之后才会发生。我会尽量让事情更清楚。

我有一个函数可以“逐个字符”比较两个字符串,以获得字符串向量的字典顺序(代码基于 Dirk Jagdmann 的“Alphanum Algorithm”实现)

int compareChar(const char *l, const char *r) {
    enum mode_t {
        STRING, NUMBER
    } mode;

    mode = STRING;

    while(*l && *r) {
        if(mode == STRING) {
            char l_char, r_char;
            while((l_char=*l) && (r_char=*r)) {
                // check if this are digit characters
                const bool l_digit=isDigit(l_char), r_digit=isDigit(r_char);
                // if both characters are digits, we continue in NUMBER mode
                if(l_digit && r_digit) {
                    mode=NUMBER;
                    break;
                }
                // if only the left character is a digit
                if(l_digit) return -1;
                // if only the right character is a digit
                if(r_digit) return +1;
                // compute the difference of both characters
                const int diff=l_char - r_char;
                // if they differ we have a result
                if(diff != 0) return diff;
                // otherwise process the next characters
                ++l;
                ++r;
    }
        } else { // mode==NUMBER
            // get the left number
            char *end;
            unsigned long l_int= strtoul(l, &end, 0);
            l=end;

            // get the right number
            unsigned long r_int= strtoul(r, &end, 0);
            r=end;

            while(*l && isDigit(*l)) {
                l_int=l_int*10 + *l-'0';
                ++l;
            }

            while(*r && isDigit(*r)) {
                r_int=r_int*10 + *r-'0';
        ++r;
            }

            // if the difference is not equal to zero, we have a comparison result
            const long diff=l_int-r_int;
            if(diff != 0) return diff;

            // otherwise we process the next substring in STRING mode
            mode=STRING;
        }
    }

    if(*r) return -1;
    if(*l) return +1;
    return 0;
}

在读取生物数据并且必须通过对象名称或符号搜索和比较对象的应用程序中多次使用此功能。名称和符号为std::string,因此使用示例为:

bool operator<(const Gene& g) const {
    if( (compareChar(chrom.c_str(), g.chrom.c_str()) < 0 ) )
        return true;
    else
        if( (compareChar(chrom.c_str(), g.chrom.c_str())) == 0 )
            if(entrez_ID == g.getId()) return true;
            else if(start_p < g.getStart()) return true;
            else return false;
        else return false;
}

具体来说,上面的 sn-p 是 operator&lt; 的重载,用于对 Gene 对象进行排序。我在代码中使用std::sort 算法

for(genes_it=chrGenes.begin(); genes_it!=chrGenes.end(); ++genes_it)
    if( ((*genes_it).getStart() > ((*conns_it).getF2Start()-const_value)) &&
            ((*genes_it).getStart() < (*conns_it).getF2Start()) )
        bf_gene.push_back((*genes_it));

if(bf_gene.size() > 1) {
    std::sort( bf_gene.begin(), bf_gene.end() );
    bf_gene.erase( std::unique(bf_gene.begin(), bf_gene.end()), bf_gene.end() );
}

嗯,它通常就像一个魅力,因为昨天我午餐时间更长的模拟,并且在运行 30 分钟后,应用程序因段错误而停止。使用 gdb 检查,这是响应:

(anonymous namespace)::compareChar (l=0x4811 <Address 0x4811 out of bounds>, r=0x9b9ec8 "chr17") at common.hpp:193
193             while(*l && *r) {

#0  (anonymous namespace)::compareChar (l=0x4811 <Address 0x4811 out of bounds>, r=0x9b9ec8 "chr17") at common.hpp:193
#1  0x0000000000410883 in Gene::operator< (this=0x8b4575b0, g=...) at Gene.hpp:222
#2  0x00000000004150a7 in std::__unguarded_partition<__gnu_cxx::__normal_iterator<Gene*, std::vector<Gene, std::allocator<Gene> > >, Gene> (__first=<value optimized out>, 
__last=<value optimized out>, __pivot=...) at /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_algo.h:2209
#3  0x0000000000415242 in std::__introsort_loop<__gnu_cxx::__normal_iterator<Gene*, std::vector<Gene, std::allocator<Gene> > >, long> (__first=..., __last=..., __depth_limit=7)
at /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_algo.h:2268
#4  0x000000000040e4e7 in sort<__gnu_cxx::__normal_iterator<Gene*, std::vector<Gene, std::allocator<Gene> > > > (this=0x620ee0, id=<value optimized out>, sc_limit=3)
at /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_algo.h:5220
#5  Gene::findConnections (this=0x620ee0, id=<value optimized out>, sc_limit=3) at Gene.cpp:1135
#6  0x000000000041a36b in main (argc=<value optimized out>, argv=<value optimized out>) at testth.cpp:30

好吧,问题显然存在,但我无法弄清楚为什么在执行 30 分钟后会发生这种情况。此外,它总是以相同的*l 内容发生(我可以从应用程序的日志文件中检查)。我不明白在这一点上它怎么可能是一个“越界”错误,只要它在数千次使用中都能正常工作。

想知道是否是std::sort算法引起的。

我会感谢所有可能的提示和建议

【问题讨论】:

  • std::string::compare 有什么问题,其次,你为什么要调用两次一般比较。重新出现错误,这显然与显示的代码无关。
  • 你的代码中有free()吗?
  • @Cheersandhth.-Alf std::string::compare 不会产生字符串的字典顺序(其中 chr2 在 chr17 之前),这就是自定义函数的原因。比较被调用了两次,因为我需要仔细检查 chrom 字段是否相等,而其他字段不同。此外,还有可能产生&gt; 0 的比较,我并不真正需要。
  • 你在哪里增加指针lr以及范围检查在哪里?
  • @Faabiioo 这不是字典的意思。

标签: c++ string algorithm c++11


【解决方案1】:

显示的代码似乎不足以解释这种行为。但是,我可以看到一些需要修复的问题。

最突出的问题是提供的operator&lt; 没有定义strict weak ordering relation

  • 不是反身的:x &lt; x 是真的;
  • 它不是不对称的:有 x 和 y,x &lt; yy &lt; x 都为真。

std::sort 需要严格的弱排序关系才能正确操作。

【讨论】:

  • 我理解你的意思,但产生的订单满足我的要求。例如,如果它必须比较 ('chr17','chr17'),它将不满足 if 语句并跳转到 else,其中处理 == 条件。此时,正如您正确掌握的那样,检查了 entrez_ID,因为它们可以相等;如果我只检查start1 &lt; start2chr1 == chr2 我没有得到我想要的顺序。我将编辑问题添加更多详细信息
  • 不满足std::sort的要求。 std::sort 不关心你的要求。
  • 我用更多细节编辑了我的问题。关于严格的弱排序关系,不使用适当的比较函数会有什么后果?我搜索了 c++ 文档,但没有指定任何内容
  • 后果是未定义的行为。 std::sort 在比较定义 SWO 的假设下工作。如果您违反该假设 std::sort 将无法正常工作,除非您很幸运。
  • 虽然我很好奇你有什么样的要求要求一个字符串小于它自己
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-25
  • 1970-01-01
  • 2011-02-07
相关资源
最近更新 更多