다음은 두 번째 함수로, 카운트 값에 따라 포인터 배열에 대한 메모리 할당 및 할당을 다시 시도합니다.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 문자열 패턴 '엘'과 일치하지 않을 때, 그것은 다음과 같은 메시지를보고 있다는 것입니다. 다른 조합에 대해서도 유사한 오류가보고됩니다. 하나가 일치하지 않는 반면 패턴과 일치하는 두 개의 문자열이있는 경우
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의에 의해보고 된 오류/누수가 없습니다. 모든 문자열이 패턴과 일치하면 확실히 손실됩니다. 잘못된 쓰기 다음에 잘못된 읽기를했습니다 고려하여
이
라인 번호를 제공해 주셔서 감사합니다. (편집 : Aaaaaand Streppel은 어떤 이유에서 그들을 제거했습니다 ....> _ <) – Izmaki
@Izmaki 죄송 합니다만, 나는 그 질문에 대한 분석에 도움이 될 것이라고는 몰랐습니다. 방금 첫 번째 개정판으로 롤백했습니다 :-) – streppel
main에서 함수 allocate_array_of_ptrs를 어떻게 호출합니까? str_array 포인터가 realloc (예를 들어 로컬 변수의 주소와 같은)에 유효하지 않습니다. – Doraj