gpt4 book ai didi

分配给 const char* 的 c++ 字符串导致地址在第 n 次迭代时越界

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:45:44 24 4
gpt4 key购买 nike

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

我有一个函数可以“逐个字符”比较两个字符串,以获得字符串 vector 的字典顺序(代码基于 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;
}

具体来说,上面的代码片段是 operator< 的重载, 用于排序 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 引起算法。

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

最佳答案

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

最引人注目的问题是 operator< provided 没有定义 strict weak ordering relation :

  • 它不是反身的:x < x是真的;
  • 它不是不对称的:x 和 y 都是 x < yy < x是真的。

std::sort需要严格的弱排序关系才能正常运行。

关于分配给 const char* 的 c++ 字符串导致地址在第 n 次迭代时越界,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24161807/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com