2013-12-12 108 views
0

以下是两个函数,其中第一个函数试图根据计数值为指针数组分配内存并随后重新分配内存。valgrind报告错误,并且在使用realloc时肯定丢失()

第二个函数试图将最后一个字符串向前连接到指针数组中的第一个字符串。

程序需要将字符串和模式的数量作为命令行参数进行匹配,并仅在输入字符串中找到匹配模式时调用第一个函数。

20 char **allocate_array_of_ptrs(char **str_array, /* pointer to the array of pointers */ 
21      char *str,    /* pointer to the input string */ 
22      int count)    /* count of matched strings */ 
23 { 
24   char **temp = NULL;   /*  temporary pointer to realloc memory */ 
25 
26   /* realloc based on count value */ 
27   temp = (char **)realloc(str_array, count * sizeof(char *)); 
28 
29   int str_len = strlen(str); 
30 
31   /* if realloc is successful */ 
32   if (NULL != temp) 
33   { 
34     str_array = temp; 
35 
36     /* alloc memory for the string to be stored */ 
37     temp[count - 1] = (char *)calloc((str_len + 1), sizeof(char)); 
38     strcpy(temp[count - 1], str); 
39   } 
40 
41   return str_array; 
42 } 
43 
44 char **dmm_str_cat(char **str_array,   /* pointer to the array of pointers */ 
45    int count)      /* count of matched strings */ 
46 { 
47   int i;           /* iterator */ 
48   int total_str_len = 0;  /* total length when all strings put together */ 
49   int str_len_of_first;  /* string length of first string in the array */ 
50 
51   if (count > 1) 
52   { 
53     str_len_of_first = strlen(str_array[0]); 
54 
55     for (i = 0; i < count; i++) 
56     { 
57       total_str_len += strlen(str_array[i]); 
58     } 
59     total_str_len += 1; 
60 
61    /* realloc memory to accomodate all matched strings onto first string */ 
62   str_array[0] = (char *)realloc(str_array[0], total_str_len * sizeof(char)); 
63 
64     /* clearing the new allocated bytes */ 
65     for (i = (str_len_of_first + 1); i < total_str_len; i++) 
66     { 
67       str_array[0][i] = '\0'; 
68     } 
69 
70     /* concatenate from the last string onwards onto first string */ 
71     for (i = count - 1; i > 0; i--) 
72     { 
73       strcat(str_array[0], str_array[i]); 
74     } 
75   } 
76 
77   return str_array; 
78 } 

的问题是,当代码下的valgrind运行,它报告下面的消息时出3串,2个字符串不匹配模式“EL”。其他组合报告了类似的错误。

 Enter string 1 : matter 

     Enter string 2 : matter 

     Enter string 3 : jello 

     Count of strings having matching pattern 'el' : 1 
     The matching strings are : 
       jello 
==6244== Invalid read of size 8 
==6244== at 0x400A74: main (dmm_main.c:86) 
==6244== Address 0x4C33038 is 0 bytes after a block of size 8 alloc'd 
==6244== at 0x4A05809: malloc (vg_replace_malloc.c:149) 
==6244== by 0x4A05883: realloc (vg_replace_malloc.c:306) 
==6244== by 0x400AE9: allocate_array_of_ptrs (dmm_functions.c:27) 
==6244== by 0x4009B6: main (dmm_main.c:64) 
==6244== 
==6244== Invalid write of size 8 
==6244== at 0x400A89: main (dmm_main.c:87) 
==6244== Address 0x4C33038 is 0 bytes after a block of size 8 alloc'd 
==6244== at 0x4A05809: malloc (vg_replace_malloc.c:149) 
==6244== by 0x4A05883: realloc (vg_replace_malloc.c:306) 
==6244== by 0x400AE9: allocate_array_of_ptrs (dmm_functions.c:27) 
==6244== by 0x4009B6: main (dmm_main.c:64) 
     The concatenated string is : jello==6244== 
==6244== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 4 from 1) 
==6244== malloc/free: in use at exit: 0 bytes in 0 blocks. 
==6244== malloc/free: 2 allocs, 2 frees, 14 bytes allocated. 
==6244== For counts of detected errors, rerun with: -v 
==6244== All heap blocks were freed -- no leaks are possible. 

但是,当有匹配的模式,而一个不匹配的两个字符串,则有由Valgrind的报告没有错误/泄漏。当所有字符串匹配模式时,肯定会丢失报告。

+1

谢谢你的行号。 (编辑:Aaaaaand Streppel删除他们出于某种原因....> _ <) – Izmaki

+1

@Izmaki对不起,我当时没有意识到,他们将有助于问题的分析。我只是回滚到第一个修订:-) – streppel

+1

你如何调用函数allocate_array_of_ptrs在你的主? 它看起来像你的str_array指针对realloc无效(例如局部变量的地址) – Doraj

回答

1

考虑到,你已经无效读取然后再无效写帐户,我敢打赌,str_array传递给allocate_array_of_ptrs没有正确initialized-

实际上,消息0 bytes after a block of size 8 alloc'd让我觉得你overpassing数组大小,但我不能100%肯定没有main.c代码。