[英]C - Valgrind “Invalid read/write of size 1” errors while strcpy/strlen
I have problem with seg.faults. 我遇到seg.faults问题。 The program works good, but for few unknown strings it results in segmentation fault.
该程序运行良好,但是对于很少的未知字符串,则会导致分段错误。 I ran the program with Valgrind and it reported "Invalid read/write of size 1", mostly the problem is connected with strcpy and strlen.
我使用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)
The program should find out whether 2 strings are made from the same words (and ignore different sizes of letters). 程序应找出两个字符串是否由相同的单词组成(并忽略不同大小的字母)。 I am very sorry for the appearence of my code, I am quite new to programming and still trying to learn how to write it understandable, so I will briefly explain what is being done (at least I think so) where.
对于我的代码的出现,我感到非常抱歉。我对编程还很陌生,但仍在尝试学习如何编写易于理解的代码,因此,我将简要解释在哪里(至少我认为是这样)进行操作。 The function WordInString takes one word after another from one string and then finds it in another.
WordInString函数从一个字符串中接一个单词,然后在另一个字符串中找到它。 The word is being copied in dynamically allocated array, because I don't know how long the words could be.
该单词被复制到动态分配的数组中,因为我不知道单词可以持续多长时间。 Then in function sameWords i copy the string in new arrays, so I could convert all the words to lower letters, and then I call function WordInString to search the strings for words.
然后在sameWords函数中,我将字符串复制到新数组中,因此我可以将所有单词转换为小写字母,然后调用WordInString函数在字符串中搜索单词。 I main function I only call sameWords with 2 strings.
我的主要功能是只用2个字符串调用sameWords。
The code looks like this. 代码看起来像这样。 Again sorry for the bad arrangement.
再次为这次安排不好对不起。
#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;
}
I would be very thankful for your help. 感谢您的帮助。 I tried to look it up, but can't figure it out.
我试图查找它,但无法弄清楚。
malloc(strlen(a)*sizeof(char));
Size +1 need for the end of string the '\\0' – BLUEPIXY 大小+1需要在字符串的末尾'\\ 0'– BLUEPIXY
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.