【发布时间】: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< 的重载,用于对 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字段是否相等,而其他字段不同。此外,还有可能产生> 0的比较,我并不真正需要。 -
你在哪里增加指针
l和r以及范围检查在哪里? -
@Faabiioo 这不是字典的意思。
标签: c++ string algorithm c++11