C (glibc),重新分配错误 "corrupted size vs. prev_size Aborted"

C (glibc), realloc error "corrupted size vs. prev_size Aborted"

我已经阅读了一些与此相关的帖子,但我是一名编码初学者,很难理解内存管理和调试过程。我觉得很困,不知道去哪里寻找答案。我真的很感激任何 help/pointers 理解这个概念和解决错误的方法。仅供参考,我什至还没有学会使用任何类型的调试 console/programs。

在这段代码中,我的目标是创建一个函数,该函数可以以只读模式打开具有给定名称的文本文件,并且 return char * 到内存地址,其中的全部内容文件。

此代码 运行 与一个简短的测试文件一起使用,但在第 12 个 运行 的 realloc 函数中始终出现“大小损坏与 prev_size 中止”错误。

下面是完整的代码和输出,包含短文本和长文本的测试 运行s。

===代码====

/////////////////////////////////////////////////////////////
/////                                                   /////
///// 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] = '[=11=]';
    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 "[=11=]";
        }
        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 "[=11=]";
                }
                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.

您没有为空终止符分配足够的 space:fContent = (char *) realloc (fContent, (strlen(fContent) * sizeof(char) + strlen(sBuffer) * sizeof(char))); 应为:

fContent = realloc(fContent, strlen(fContent) + strlen(sBuffer) + 1);

此外,您应该将对 fclose(fRead); 的调用移动到 if 块的末尾:如编码所示,如​​果打开文件失败,fclose(fRead) 将具有未定义的行为。

重新调整字符串文字 "[=16=]",它应该只是 "",这是有问题的,因为调用者应该对结果调用 free()。您应该 return NULL 来指示错误。

另读Why is “while ( !feof (file) )” always wrong?

这是修改后的版本:

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifndef STR_BUFFER
    #define STR_BUFFER 25000
#endif

char *fopenRfixedsize(const char *fName);

int main(int argc, char *argv[])
{
    if (argc != 2) // input limited to single file name only
    {
        printf("Usage: ./fopenRfixedsize filename\n");
        return 1;
    }
    printf("%s", fopenRfixedsize(argv[1]));
    return 0;
}

// parent, sole primary function for fopenRfixedsize()
char *fopenRfixedsize(const char *fName)
{
    FILE *fRead = fopen(fName, "r"); // open file
    if (fRead == NULL) {
        printf("Cannot open file %s: %s\n", fName, strerror(errno));
        return NULL;
    }
    // allocate memory space to read file, fixed size #define STR_BUFFER 'size'
    char *sBuffer = (char *)malloc(STR_BUFFER * sizeof(char)); 
    if (sBuffer == NULL) {
        printf("Memory allocation error (sBuffer): %s\n", strerror(errno));
        fclose(fRead);
        return NULL;
    }
    char *fContent = (char *)malloc(1);
    if (fContent == NULL) {
        printf("Memory allocation error (fContent): %s\n", strerror(errno));
        free(sBuffer);
        fclose(fRead);
        return NULL;
    }
    size_t len = 0;
    fContent[len] = '[=11=]';

    while (fgets(sBuffer, STR_BUFFER, fRead)) {
       size_t len1 = strlen(sBuffer);
       fNewContent = (char *)realloc(fContent, len + len1 + 1);
       if (fNewContent == NULL) {
           printf("Memory reallocation error (fContent): %s\n", strerror(errno)); 
           free(fContent);
           free(sBuffer);
           fclose(fRead);
           return NULL;
       }
       fContent = fNewContent
       strcpy(fContent + len, sBuffer);
       len += len1;
    }
    free(sBuffer);
    fclose(fRead);
    return fContent;
}