gpt4 book ai didi

c - OpenMP 循环上的段错误(核心转储)

转载 作者:行者123 更新时间:2023-11-30 14:53:11 25 4
gpt4 key购买 nike

我有一个算法,可以在另一个字符串中查找一个字符串,并且我得到了一些很长的字符串:

Segmentation fault (core dumped)

这是我的代码,其中: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;
}

最佳答案

我在您的代码中发现了两个拼写错误:

错误的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;

关于c - OpenMP 循环上的段错误(核心转储),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47347664/

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