![](/img/trans.png)
[英]I am pretty sure I have a memory leak in a recursive function but I don't know how to fix it
[英]I tried to make an array of strings and now I have an issue with my code which I don't know how to fix
我試圖通過使用指針來創建一個字符串數組,但是由於某種原因(我希望您會知道),我鍵入第二個字符串后程序崩潰了。 我已經嘗試了數小時,以查找此代碼出了什么問題,希望能對您有所幫助! 在調試期間,重新分配輸入后,它顯示“讀取字符串的錯誤”。 這是代碼:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
int main()
{
char **input = (char **)calloc(1, sizeof(char)), ch;
*input = (char *)calloc(1, sizeof(char));
int size = 1,sizeinput=1,current = 0,currentarr=0,i=0,j=0;
while (i<5)
{
printf("please enter a word\n");
scanf("%c", &ch);
while (ch != '\n')
{
*(input+i) = (char*)realloc(*(input+i), (size++)*sizeof(char));
*(*(input + i) + j++) = ch;
scanf("%c", &ch);
}
*(*(input + i) + j) = '\0';
j = 0;
input = (char**)realloc(input, (sizeinput++)*sizeof(char*));
i++;
}
free(input);
}
(char **)calloc(1, sizeof(char))
應該
(char **)calloc(1, sizeof(char*))
同樣,在重新分配input
,最后一個元素是未初始化的指針! 在循環的末尾添加它,這樣它指向有用的東西,以后可以重新分配:
input[sizeinput - 1] = calloc(1, sizeof(char));
外觀問題:
無需calloc
和realloc
的結果-盡管這樣做不會阻止程序正常運行,但確實會使程序更難看。
另外,您可以使用input[i]
代替*(input + i)
和input[i][j]
代替*(*(input + i) + j)
-同樣,它不會影響程序的執行方式,但可以使其更易於閱讀。
由於未分配的內存中發生重新分配,因此出現了問題。 考慮您的原始代碼。 第一次調用realloc
時,您將它稱為*(input + i)
,因為之前i
為零,所以先前已對其進行了分配。 這種分配發生在main
函數的第二行。 問題在於,您永遠不會分配位置1,您稍后會嘗試重新分配它。 可以解決此問題:
int main()
{
char **input = (char **)calloc(1, sizeof(char)), ch;
// Don't do it here
//*input = (char *)calloc(1, sizeof(char));
int size = 1,sizeinput=1,current = 0,currentarr=0,i=0,j=0;
while (i<5)
{
printf("please enter a word\n");
scanf("%c", &ch);
// Do it here instead
*(input + i) = (char *)calloc(1, sizeof(char));
while (ch != '\n')
{
// Now this reallocation is actually being given something that was allocated before
*(input+i) = (char*)realloc(*(input+i), (size++)*sizeof(char));
*(*(input + i) + j++) = ch;
scanf("%c", &ch);
}
*(*(input + i) + j) = '\0';
j = 0;
input = (char**)realloc(input, (sizeinput++)*sizeof(char*));
i++;
}
free(input);
}
與此相關的還有其他一些問題,但是由於我的第一個建議是采用其他方法,因此我將不再討論其他問題。 無論如何,請讓我知道。
注意:這會導致每個循環額外分配一個。 但是正如我所說,這只是一個快速修復。
下面的代碼合並了注釋,進行了干凈地編譯,使用了一個函數: cleanup()
我未定義,但是cleanup()
會將每個單詞傳遞給free()
然后將“指向char的指針數組”傳遞給free()
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//#include <conio.h>
// prototypes
void cleanup( char ** input );
int main()
{
char **input = calloc(1, sizeof(char*));
char ch;
*input = calloc(1, sizeof(char));
size_t size = 1;
size_t sizeinput=1;
int i=0;
int j=0;
while (i<5)
{
printf("please enter a word\n");
if( 1 != scanf("%c", &ch) )
{
perror( "scanf failed" );
cleanup( input );
exit( EXIT_FAILURE );
}
while (ch != '\n')
{
size++;
char *temp = realloc(*(input+i), size);
if( NULL == temp)
{
perror( "realloc for word failed" );
cleanup( input );
exit( EXIT_FAILURE );
}
*(input+i) = temp;
*(*(input + i) + j) = ch;
j++;
if( 1 != scanf("%c", &ch) )
{
perror( "scanf failed" );
cleanup( input );
exit( EXIT_FAILURE );
}
}
// NUL terminate word
*(*(input + i) + j) = '\0';
j = 0;
sizeinput++;
char** lineTemp = realloc(input, sizeinput*sizeof(char*));
if( NULL == lineTemp )
{
perror( "realloc failed" );
cleanup( input );
exit( EXIT_FAILURE );
}
input = lineTemp;
i++;
}
free(input);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.