我遇到了seg.faults的问题。该程序运行良好,但对于很少的未知字符串,它会导致分段错误。我用Valgrind运行程序,它报告“无效的读/写大小1”,大多数问题与strcpy和strlen有关。
==5623== ERROR SUMMARY: 12 errors from 4 contexts (suppressed: 2 from 2)
==5623==
==5623== 1 errors in context 1 of 4:
==5623== Invalid write of size 1
==5623== at 0x4C2D812: strcpy (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5623== by 0x400955: sameWords (main.c:62)
==5623== by 0x400A6F: main (main.c:85)
==5623== Address 0x51fd093 is 0 bytes after a block of size 3 alloc'd
==5623== at 0x4C2CD7B: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5623== by 0x40092B: sameWords (main.c:59)
==5623== by 0x400A6F: main (main.c:85)
==5623==
==5623==
==5623== 1 errors in context 2 of 4:
==5623== Invalid write of size 1
==5623== at 0x4C2D812: strcpy (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5623== by 0x400942: sameWords (main.c:61)
==5623== by 0x400A6F: main (main.c:85)
==5623== Address 0x51fd045 is 0 bytes after a block of size 5 alloc'd
==5623== at 0x4C2CD7B: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5623== by 0x400913: sameWords (main.c:58)
==5623== by 0x400A6F: main (main.c:85)
==5623==
==5623==
==5623== 4 errors in context 3 of 4:
==5623== Invalid read of size 1
==5623== at 0x4C2D7B4: strlen (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5623== by 0x4009EC: sameWords (main.c:70)
==5623== by 0x400A6F: main (main.c:85)
==5623== Address 0x51fd093 is 0 bytes after a block of size 3 alloc'd
==5623== at 0x4C2CD7B: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5623== by 0x40092B: sameWords (main.c:59)
==5623== by 0x400A6F: main (main.c:85)
==5623==
==5623==
==5623== 6 errors in context 4 of 4:
==5623== Invalid read of size 1
==5623== at 0x4C2D7B4: strlen (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5623== by 0x40099E: sameWords (main.c:65)
==5623== by 0x400A6F: main (main.c:85)
==5623== Address 0x51fd045 is 0 bytes after a block of size 5 alloc'd
==5623== at 0x4C2CD7B: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5623== by 0x400913: sameWords (main.c:58)
==5623== by 0x400A6F: main (main.c:85)
==5623==
--5623--
--5623-- used_suppression: 2 dl-hack3-cond-1
==5623==
==5623== ERROR SUMMARY: 12 errors from 4 contexts (suppressed: 2 from 2)
程序应该找出2个字符串是否由相同的单词组成(并忽略不同大小的字母)。我很遗憾我的代码的出现,我对编程很新,并且仍然试图学习如何编写它是可以理解的,所以我将简要解释正在做什么(至少我是这么认为)。 WordInString函数从一个字符串中取一个接一个的字,然后在另一个字符串中找到它。这个词是在动态分配的数组中复制的,因为我不知道这些单词可能有多长。然后在函数sameWords中我复制新数组中的字符串,所以我可以将所有单词转换为低位字母,然后我调用函数WordInString来搜索字符串中的单词。我的主要功能我只用2个字符串调用sameWords
代码看起来像这样。再次抱歉这个糟糕的安排。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
int WordInString(const char *a, const char *b)
{
/* Selecting words one after another and finding the word in another strin.
Dynamic allocation of word string, because words lengths are unknown.*/
char * word=NULL;
int previous = 0;
int length=0;
int wordlength=0;
int i;
if((strlen(a)==0) && (strlen(b)==0))
{
return 1;
}
i=0;
while(1)
{
if (i>=wordlength) {wordlength+=250; word=(char*) malloc(wordlength*sizeof(char));}
if(isalpha(a[i]))
{
if(!isalpha(previous))
{
length=0;
}
if(length<80) word[length++] = tolower(a[i]);
}
else
{
if(length>0)
{
word[length] = '\0';
if(strstr(b, word)==NULL)
{
return 0;
}
length=0;
}
}
if(a[i] == '\0') {break;}
previous=tolower(a[i++]);
}
free(word);
return 1;
}
int sameWords( const char * a, const char * b)
{
int i=0, j=0;
char * array1=(char*) malloc(strlen(a)*sizeof(char));
char * array2=(char*) malloc(strlen(b)*sizeof(char));
/* copy a and b strings to new string, that are not cons, so they can be changed tolower */
strcpy(array1, a);
strcpy(array2, b);
/* convert strings to lower letters */
for(i=0; i<strlen(array1); i++)
{
array1[i]=tolower(array1[i]);
}
for(j=0; j<strlen(array2); j++)
{
array2[j]=tolower(array2[j]);
}
/* calling WordInString to compare */
if (WordInString(a, array2)==0) {return 0;}
if (WordInString(b, array1)==0) {return 0;}
free(array1);
free(array2);
return 1;
}
int main ( int argc, char * argv [] )
{
int res;
res=(sameWords("This is a string", "This string is a string"));
return 0;
}
我非常感谢你的帮助。我试着查一查,但无法理解。
答案 0 :(得分:1)
malloc(strlen(a)*sizeof(char));
大小+1需要字符串结尾&#39; \ 0&#39; - BLUEPIXY