Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 分段故障(堆芯转储)…BMH算法_C_Memory Leaks_Segmentation Fault - Fatal编程技术网

C 分段故障(堆芯转储)…BMH算法

C 分段故障(堆芯转储)…BMH算法,c,memory-leaks,segmentation-fault,C,Memory Leaks,Segmentation Fault,我正在尝试实现一个Boyer-Moore-Horsepoole算法。这个代码是用Turbo C++,Windows编写的。成功了。我必须把它移植到ubuntu中 typedef struct skip_table { char index; int value; }skip_table; void create_table(char*,int); int discrete_char(char*,int); int bm(char*, char*); int lookup(cha

我正在尝试实现一个Boyer-Moore-Horsepoole算法。这个代码是用Turbo C++,Windows编写的。成功了。我必须把它移植到ubuntu中

typedef struct skip_table
{
    char index;
    int value;
}skip_table;

void create_table(char*,int);
int discrete_char(char*,int);
int bm(char*, char*);
int lookup(char);
int check_EOF(char*,int);

skip_table *t1;
int tab_len;
FILE *fptr;

int main()
{
    time_t first, second;
    double time_spent;
    long int cnt=0;

    char *key_string,*buf,c; // String to be matched and text
    int i,key_len,text_len,def_shift_len,flag_match=0;

    gets(key_string);
    key_len=strlen(key_string);

    fptr=fopen("test_file.txt","r");
    first = clock();
    fseek(fptr,SEEK_SET,0);
    create_table(key_string,key_len);

    while(flag_match!=1)
    {
        fseek(fptr,100*cnt,0);
        fread(buf,100-key_len-1, 1, fptr);
        flag_match = bm(buf, key_string);
        cnt++;

    printf("\n%d",cnt);
     }
    second =clock();
time_spent=(double)(second-first)/CLOCKS_PER_SEC;

if(flag_match==1)
    printf("\n\nMatch Found in %lf seconds",time_spent);
else
    printf("\n\nMatch NOT Found in %lf seconds",time_spent);

    fclose(fptr);
    return 0;
}

int discrete_char(char* key_string,char* temp,int key_len)
{
    int i,j,count=1,flag=0;

for(i=1;i<key_len;i++)
{
    for(j=0; j<count; j++)
    {
        flag=0;
        if(temp[j] == key_string[i])
        {
            flag=1;
            break;
        }
    }
        if(flag!=1)
        {
            temp[count++]=key_string[i];
            flag=0;
        }
}

temp[count]='\0';
return count;
}

void create_table(char* key_string,int key_len)
{
    int i,j,k,max_index;
char *temp;
temp[0] = key_string[0];

tab_len=discrete_char(key_string,temp,key_len);
t1=(skip_table*)malloc((tab_len-1)*sizeof(skip_table));

for(i=0;i<tab_len;i++)
{
    for(j=0;j<key_len;j++)
    {
        if(temp[i]==key_string[j])
            max_index=j;
    }

    t1[i].index=temp[i];
    t1[i].value=key_len-max_index-1;

    printf("\n\n %c %d",t1[i].index,t1[i].value);
}
}

int bm(char* text, char* key_string)
{
int i_t, i_k, j,k, text_len, key_len, shift, count=0, flag_match=0;
int loop_count;

text_len = strlen(text);
key_len = strlen(key_string);
i_t=key_len;
i_k=key_len;

loop_count=0;

while(i_t<=text_len)
{
    if(count != key_len)
    {
        if(text[i_t-1]==key_string[i_k-1])
        {
            count++;
            i_t--;  i_k--;
            loop_count++;
        }
        else
        {
            if(loop_count>key_len)
            {
                i_t=i_t+lookup(text[i_t-1])+1;
                i_k=key_len;
                loop_count=0;
                continue;
            }
            shift = lookup(text[i_t-1]);
            if(shift<=0)
                shift=key_len;
            i_t = i_t+shift;
            i_k = key_len;
            count=0;
        }
    }
    else
    {
        flag_match =  1;
        break;
    }
}
return flag_match;
}
typedef结构跳过表
{
煤焦指数;
int值;
}跳台;
void创建_表(char*,int);
int离散字符(字符*,int);
int bm(字符*,字符*);
int查找(char);
整数校验(字符*,整数);
跳过表*t1;
int tab_len;
文件*fptr;
int main()
{
时间第一,第二;
花费的时间加倍;
长int cnt=0;
char*key_string,*buf,c;//要匹配的字符串和文本
int i,key_len,text_len,def_shift_len,flag_match=0;
获取(键字符串);
密钥长度=strlen(密钥字符串);
fptr=fopen(“test_file.txt”,“r”);
第一个=时钟();
fseek(fptr,SEEK_SET,0);
创建表(键字符串,键len);
while(flag_match!=1)
{
fseek(fptr,100*cnt,0);
fread(buf,100-key_len-1,1,fptr);
标志匹配=bm(buf,键字符串);
cnt++;
printf(“\n%d”,cnt);
}
秒=时钟();
花费的时间=(双倍)(秒优先)/每秒时钟;
如果(标志_匹配==1)
printf(“\n\n在%lf秒内找到匹配项”,所用时间);
其他的
printf(“\n\n在%lf秒内未找到匹配项”,所用时间);
fclose(fptr);
返回0;
}
整数离散字符(字符*键\字符串,字符*临时,整数键\长度)
{
int i,j,计数=1,标志=0;

对于(i=1;i而言,我并没有完全看出哪里出了问题,但这里有一些防御性编程技巧:

int main()
{
  // initialize all variables before use
  time_t first = 0, second = 0;
  double time_spent = 0.0;
  long int cnt=0;

  char *key_string = NULL;
  char *buf = NULL;
  char  c = '\0';  
  char temp[50] = {0};
  int i = 0,key_len=0,text_len=0,def_shift_len=0,flag_match=0;

// use fgets instead of gets, fgets allows you specify max length

  fgets(temp,sizeof(temp),stdin);
  key_len=strlen(temp);
  key_string = (char*) malloc(key_len+1);

// use strncpy or strcpy_s to specify max size
  strncpy(key_string, temp, sizeof(key_string));

  fptr = fopen("test_file.txt","r");

  first = clock();

// here arguments have wrong order, fseek takes origin as last arg:
  fseek(fptr,0,SEEK_SET);

// could be something in create_table, but you have not supplied it
  create_table(key_string,key_len);

当你在函数中有这么多变量时,你可以考虑把函数的一部分移到其他函数

尝试使用<代码> -跟踪起源=“是/代码”在你的ValGRD选项上,正如输出建议的那样,这可以帮助追踪未初始化的变量是从哪里来的。


正如其他人所建议的,valgrind正在报告的问题在
create_table
中,因此也请发布该问题的代码。

create_table()
做什么?添加定义。.您确定调用
gets()时没有溢出
temp
哪个没有边界检查?提示:使用调试选项编译(通常是
-g
)然后你会在
valgrind
输出中出现问题的地方得到行号,这样更容易发现问题。你的代码在一个你没有展示给我们的函数中崩溃了。你希望我们如何为你调试它?我同意使用
fgets()
得到()的时间已经过去很久了
被遗忘了。我看不出在这里使用
strncpy()
的原因;您只测量了字符串的长度,为该长度和null分配了足够的空间,因此不需要检查字符串副本。您是否建议
memmove(key_string,temp,key_len+1)
(或
memcpy()
),那么肯定是的。独立于此,
strncpy(key_string,temp,sizeof(key_string));
是错误的;它应该是
strncpy(key_string,temp,key_len+1);
。很好地发现了
fseek()的乱序参数
;如果你注意到这是不必要的,你会得到更多的分数,因为文件一开始就被打开阅读。@JonathanLeffler是的,我同意abt
strncpy
,strcpy很好,我已经编辑了答案。关于
fseek
也是如此,尽管他以前的经历可能会导致问题-我也不确定他是否显示了所有代码。嘿非常感谢大家..解决了问题..我想Turbo C是一个非常简单的工具,不会检查这些故障..以后会小心..谢谢..)