我有一个算法,在另一个字符串中查找一个字符串,并且我得到了一些非常长的字符串:
分段错误(核心转储)
这是我的代码,其中: S是要查找的字符串,B是可能包含S字符串的大字符串
int search( char *S, int sizeS, char *B, int sizeB )
{
int result = -1;
#pragma omp parallel shared(result)
{
int startB, thread, threads, length;
threads = omp_get_num_threads();
thread = omp_get_thread_num();
length = sizeB / threads;
for(startB = length * thread; result==-1 && startB <= startB+length; startB++ ) {
int ind;
for( ind = 0; ind < sizeS; ind++ ) {
if ( S[ind] != B[startB+ind] ) break;
}
if ( ind == sizeS && result == -1)
result = startB;
}
}
return result;
}
答案 0 :(得分:1)
我在你的代码中发现了两个拼写错误:
错误的for
循环条件(导致崩溃):
for(startB = length * thread; result==-1 && startB <= startB ; startB++ )
应该是:
for(startB = length * thread; result==-1 && startB <= sizeB ; startB++ )
然后,ind
测试似乎是错误的(阻止查找子字符串):
if ( ind == sizeS && result == -1)
result = startB;
应该是
if ( ind == (sizeS - 1) && result == -1)
result = startB;