【发布时间】:2022-01-06 22:01:48
【问题描述】:
我已经阅读了一些关于此的帖子,但我是一名初学者,很难理解内存管理和调试过程。我感觉很卡,不知道在哪里寻找答案。我非常感谢任何帮助/指针来理解这个概念和解决错误的方法。仅供参考,我什至还没有学会使用任何类型的调试控制台/程序。
在这段代码中,我的目标是创建一个函数,该函数可以以只读模式打开具有给定名称的文本文件,并将 char * 与文件的全部内容一起返回到内存地址。
此代码使用简短的测试文件按预期运行,但在第 12 次 realloc 函数运行时始终出现“损坏的大小与 prev_size Aborted”错误。
下面是完整的代码和输出,包括短文本和长文本的测试运行。
=== 代码 ====
/////////////////////////////////////////////////////////////
///// /////
///// FILE I/O
///// Character Type, Read only mode
///// Fixed Buffer Size by #define STR_BUFFER size
///// /////
/////////////////////////////////////////////////////////////
// USE: ./fopenRfixedsize argument
// Example: ./fopenRfixedsize filename
// Description:
// - argument is the filename
// - only one argument allowed
// ,or char *fopenRfixedsize(char *fName[]);
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#ifndef STR_BUFFER
#define STR_BUFFER 25000
#endif
FILE *fRead;
char *fopenRfixedsize(char *fName[]);
///////////////////////////////////////////////////////////////////////////////////////////////////
// temp main function for individual module testing
int main(int argc, char *argv[])
{
if (argc != 2) // input limited to single file name only
{
printf("Usage: ./fopenRfixedsize filename\n");
exit (1);
}
printf("%s", fopenRfixedsize(&argv[1]));
return 0;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// parent, sole primary function for fopenRfixedsize()
char *fopenRfixedsize(char *fName[])
{
errno = 0; // initialize errno
char *fContent = (char *) malloc (sizeof(char));
fContent[0] = '\0';
fRead = fopen(*fName, "r"); // open file
if (fRead) // if fopen returned value other than 0, file open successful.
{
// create memory space to read file, fixed size #define STR_BUFFER 'size'
char *sBuffer = (char *) malloc (STR_BUFFER * sizeof(char));
if(sBuffer == 0)
{
printf("Memory allocation error (sBuffer): %d\n", errno);
free(sBuffer);
fclose(fRead);
return "\0";
}
while(!feof(fRead)) // feof is not 0 == fRead is not at EOF (End of File)
{
// read file and store to the buffer,
// - until fRead points EOF or until string size reaches STR_BUFFER
fgets(sBuffer, STR_BUFFER, fRead);
if (!feof(fRead)) // if feof == 0, fRead is at EOF (End of File)
{
//////////////////////////////////////////////////////////////////////////////////////////////////////
// debug line, 1/3
printf("==========================\n");
printf("=== Beginning of Cycle ===\n");
printf("==========================\n");
printf("strlen(fContent): %d, strlen(sBuffer): %d\n", (int)strlen(fContent), (int)strlen(sBuffer));
printf("fContent: %s", fContent);
if (strlen(fContent) == 0) printf("\n");
printf("sBuffer: %s", sBuffer);
printf("\n");
// debug line ends, 1/3
//////////////////////////////////////////////////////////////////////////////////////////////////////
fContent = (char *) realloc (fContent, (strlen(fContent) * sizeof(char) + strlen(sBuffer) * sizeof(char)));
//////////////////////////////////////////////////////////////////////////////////////////////////////
// debug line, 2/3
printf("realloc with size %d successful\n", (int)(strlen(fContent) * sizeof(char) + strlen(sBuffer) * sizeof(char)));
// debug line ends, 2/3
//////////////////////////////////////////////////////////////////////////////////////////////////////
if(fContent == 0)
{
printf("Memory allocation error (fContent): %d\n", errno);
free(fContent);
free(sBuffer);
fclose(fRead);
return "\0";
}
strcat(fContent, sBuffer);
/////////////////////////////////////////////////////////////////////////////////////////////////////
// debug line, 3/3
printf("strcat(fContent, sBuffer) complete\n");
printf("\n");
printf("fContent: %s", fContent);
printf("sBuffer: %s", sBuffer);
printf("==========================\n");
printf("=== End of Cycle ===\n");
printf("==========================\n");
printf("\n");
// debug line end, 3/3
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
free(sBuffer);
}
else // of fopen returns 0, NULL pointer
{
printf("Cannot open the file ");
printf("(Error Number: %d)\n", errno); // print errno message for debug
}
fclose(fRead); // close open files prior exiting program.
return fContent;
}
=== 输出 1,小尺寸文本文件 ===
./fopenRfixedsize ftest.txt
==========================
=== Beginning of Cycle ===
==========================
strlen(fContent): 0, strlen(sBuffer): 42
fContent:
sBuffer: Really trying to get this thing work.....
realloc with size 42 successful
strcat(fContent, sBuffer) complete
fContent: Really trying to get this thing work.....
sBuffer: Really trying to get this thing work.....
==========================
=== End of Cycle ===
==========================
==========================
=== Beginning of Cycle ===
==========================
strlen(fContent): 42, strlen(sBuffer): 42
fContent: Really trying to get this thing work.....
sBuffer: Why is this so hard for me .... grrr.....
realloc with size 84 successful
strcat(fContent, sBuffer) complete
fContent: Really trying to get this thing work.....
Why is this so hard for me .... grrr.....
sBuffer: Why is this so hard for me .... grrr.....
==========================
=== End of Cycle ===
==========================
Really trying to get this thing work.....
Why is this so hard for me .... grrr.....
=== 输出2,大尺寸文本文件===
./fopenRfixedsize sorted5000.txt
==========================
=== Beginning of Cycle ===
==========================
strlen(fContent): 0, strlen(sBuffer): 2
fContent:
sBuffer: 1
realloc with size 2 successful
strcat(fContent, sBuffer) complete
fContent: 1
sBuffer: 1
==========================
=== End of Cycle ===
==========================
==========================
=== Beginning of Cycle ===
==========================
strlen(fContent): 2, strlen(sBuffer): 2
fContent: 1
sBuffer: 2
realloc with size 4 successful
strcat(fContent, sBuffer) complete
fContent: 1
2
sBuffer: 2
==========================
=== End of Cycle ===
==========================
==========================
=== Beginning of Cycle ===
==========================
strlen(fContent): 4, strlen(sBuffer): 2
fContent: 1
2
sBuffer: 3
realloc with size 6 successful
strcat(fContent, sBuffer) complete
fContent: 1
2
3
sBuffer: 3
==========================
=== End of Cycle ===
==========================
==========================
=== Beginning of Cycle ===
==========================
strlen(fContent): 6, strlen(sBuffer): 2
fContent: 1
2
3
sBuffer: 4
realloc with size 8 successful
strcat(fContent, sBuffer) complete
fContent: 1
2
3
4
sBuffer: 4
==========================
=== End of Cycle ===
==========================
==========================
=== Beginning of Cycle ===
==========================
strlen(fContent): 8, strlen(sBuffer): 2
fContent: 1
2
3
4
sBuffer: 5
realloc with size 10 successful
strcat(fContent, sBuffer) complete
fContent: 1
2
3
4
5
sBuffer: 5
==========================
=== End of Cycle ===
==========================
==========================
=== Beginning of Cycle ===
==========================
strlen(fContent): 10, strlen(sBuffer): 2
fContent: 1
2
3
4
5
sBuffer: 6
realloc with size 12 successful
strcat(fContent, sBuffer) complete
fContent: 1
2
3
4
5
6
sBuffer: 6
==========================
=== End of Cycle ===
==========================
==========================
=== Beginning of Cycle ===
==========================
strlen(fContent): 12, strlen(sBuffer): 2
fContent: 1
2
3
4
5
6
sBuffer: 7
realloc with size 14 successful
strcat(fContent, sBuffer) complete
fContent: 1
2
3
4
5
6
7
sBuffer: 7
==========================
=== End of Cycle ===
==========================
==========================
=== Beginning of Cycle ===
==========================
strlen(fContent): 14, strlen(sBuffer): 2
fContent: 1
2
3
4
5
6
7
sBuffer: 8
realloc with size 16 successful
strcat(fContent, sBuffer) complete
fContent: 1
2
3
4
5
6
7
8
sBuffer: 8
==========================
=== End of Cycle ===
==========================
==========================
=== Beginning of Cycle ===
==========================
strlen(fContent): 16, strlen(sBuffer): 2
fContent: 1
2
3
4
5
6
7
8
sBuffer: 9
realloc with size 18 successful
strcat(fContent, sBuffer) complete
fContent: 1
2
3
4
5
6
7
8
9
sBuffer: 9
==========================
=== End of Cycle ===
==========================
==========================
=== Beginning of Cycle ===
==========================
strlen(fContent): 18, strlen(sBuffer): 3
fContent: 1
2
3
4
5
6
7
8
9
sBuffer: 10
realloc with size 21 successful
strcat(fContent, sBuffer) complete
fContent: 1
2
3
4
5
6
7
8
9
10
sBuffer: 10
==========================
=== End of Cycle ===
==========================
==========================
=== Beginning of Cycle ===
==========================
strlen(fContent): 21, strlen(sBuffer): 3
fContent: 1
2
3
4
5
6
7
8
9
10
sBuffer: 11
realloc with size 24 successful
strcat(fContent, sBuffer) complete
fContent: 1
2
3
4
5
6
7
8
9
10
11
sBuffer: 11
==========================
=== End of Cycle ===
==========================
==========================
=== Beginning of Cycle ===
==========================
strlen(fContent): 24, strlen(sBuffer): 3
fContent: 1
2
3
4
5
6
7
8
9
10
11
sBuffer: 12
corrupted size vs. prev_size
Aborted
======
感谢您抽出宝贵时间查看此内容 我期待着从你那里得到一些建议。
真诚的。 J.
【问题讨论】:
-
通过 valgrind 运行您的代码。如果你对内存管理不善,它会告诉你在哪里。
-
字符串连接调整大小除了两个字符串的长度外,还需要为终止符添加一个额外的槽空间。你 realloc 只包括两个字符串长度(不考虑终止 nulchars)。因此,
strcat将保证将一个字符爆破到未计入的空间中,从而调用 未定义的行为。realloc的大小应为1 + strlen(fContent) + strlen(sBuffer)(因为sizeof(char)始终为1,除非您喜欢输入晦涩难懂的代码,否则无需包含它)。 -
可能不相关,但返回静态字符串 (
"\0") 或从同一函数动态分配的字符串是一个非常糟糕的主意,因为调用者需要释放它,但万一它是静态的,free将失败。 -
感谢大家的cmets! WhozCraig,感谢您的描述并为我指出“sizeof(char) == 1”...,我的慢大脑无法捕捉到的东西...????