我遇到了一些與此代碼有關的問題,並且會介意獲得一些幫助。此功能從文件讀取到動態分配的內存從文件讀取字符數組,C
感謝@JonathanLeffler尋求幫助 - 功能縮進功能完美!但是又出現了一個問題:使用函數read_file從文件讀取到char數組,稍後傳遞給縮進。
============================================== ===========================
//--------------- read_file valgrind validations --------------------
==396== 144 bytes in 1 blocks are definitely lost in loss record 62 of 66
==396== at 0x4C2AD10: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==396== by 0x401AC1: read_file (polisher.c:24)
==396== by 0x4025CE: test_indent (test_source.c:174)
==396== by 0x406BC7: srunner_run (in /tmc/test/test)
==396== by 0x402C67: tmc_run_tests (tmc-check.c:134)
==396== by 0x402902: main (test_source.c:235)
==396==
================ =====================================
char *read_file(const char *filename)
{
FILE *f = fopen(filename, "r");
if(!f)
return NULL;
int n = 0, c = 0;
char *a = NULL;
c = fgetc(f);
while(c != EOF)
{
n++;
c = fgetc(f);
}
freopen(filename, "r", f);
a = calloc(n + 1, sizeof(char));
c = fgetc(f);
n = 0;
while(c != EOF)
{
a[n] = c;
n++;
c = fgetc(f);
}
a[n] = '\0';
fclose(f);
return a;
}
====== ================================================== ========
START_TEST(test_indent)
{
char *str = read_file("testifile.c");
if (!str) str = read_file("test/testifile.c");
if (!str) {
fail("[M6.01.c] read_file(\"testifile.c\") returned NULL");
}
char *res = indent(str, " ");
if (!res) {
free(str);
free(res);
fail("[M6.01.c] indent(\"testifile.c\") returned NULL");
}
char buf[OUTPUTLEN];
if (mycompare_new(res, ref61c, buf, OUTPUTLEN)) {
free(res);
free(str);
fail("[M6.01.c] Invalid string from indent(\"testifile.c\"): %s", buf);
}
free(str);
free(res);
test_complete();
}
END_TEST
的就是你得到的第一個問題的錯誤?對於valgrind輸出,你能指出哪一行出現錯誤(哪一行代碼是行116,127,...)? – Garf365
@ Garf365'strncpy(dest + dest_offset,pad,pad_len + 1);'是116.'dest [dest_offset ++] = c; '127。當我嘗試發送這個函數到服務器時,它說「提前返回值1」。第一個問題的錯誤信息是「接收信號:SIGABRT(中止)。對於主,PID 9424」 – JasonUrban
請編輯您的問題以添加此信息。此外,檢查每次函數'indent'被提及到valgrind輸出中,並添加關於每行的信息 – Garf365