无法分配内存错误:fgetc 或 fgets

编程


我编写了一个简单的c程序,如下所示,我们尝试使用 fgets() 逐行读取数据,并且跳过不以字符 ‘$’ 开头的行,我面临的问题是我fgets 出现错误:无法在第 4 行分配内存,并且它是随机发生的

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

#define MAX_BUFFER_SIZE_5000 5000

size_t CustomGetLine(char *dPtr, size_t *n, FILE *stream, char cStartChar,uint32_t offset )
{
    char *result;

    // Read a line from the stream using fgets
    result = fgets(dPtr, *n, stream);
    perror("fgets");

    if (result != NULL)
    {
        // Increment the offset by the length of the line
        offset += strlen(dPtr);
        
        // Check if the first character of the line matches cStartChar
        if (dPtr[0] != cStartChar)
        {
            // Skip this line by reading the next one
            return CustomGetLine(dPtr, n, stream, cStartChar, offset);
        }
        
        // Return the length of the string read
        return strlen(dPtr);
    }


    return 0; // Return 0 if fgets fails
}

int main()
{
    uint32_t u32Offset = 0, uPktlen = 0;
    size_t PtrSize = MAX_BUFFER_SIZE_5000;
    FILE *pCurrFile = fopen("test.txt", "r");
    
    char *tempbuf = (char *)malloc(MAX_BUFFER_SIZE_5000);
    
    if (pCurrFile != NULL && tempbuf != NULL)
    {
        //printf("File opened successfully.\n");
        fseek(pCurrFile, u32Offset, SEEK_SET);
        
        
        while (uPktlen = CustomGetLine(tempbuf, &PtrSize, pCurrFile, '$', u32Offset))
        {
            printf("uPktlen before 0 = %d\n", uPktlen);
            printf("%s\n", tempbuf);
            u32Offset += uPktlen;
        }
        printf("uPktlen before 0 = %d\n", uPktlen);
        
        fclose(pCurrFile);
    }
    else
    {
        printf("Error: Unable to open file or allocate memory.\n");
    }
    
    if (tempbuf != NULL)
    {
        free(tempbuf);
        tempbuf = NULL;
    }

    return 0;
}

文件数据是:

$BDEFGHOP,2.3,307,T139226500,1,060324130447,685,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0.0000,0.0000,0.0000,0.0000,0.0000,0.0000,0.0000,0.0000,0.00,0.00,37.00,30,1,16,1,0,1,0,1,0,0,0,0,0,0,0,1,0,0,0,5,20,NONE,1,2,0,0,0,0.00,0.00,0.000,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,1328,0,0.00,0.00,0.00,20,NONE,1,2,0,0,0,0.00,0.00,0.000,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0,0,0.00,0.00,0.00,20,NONE,1,2,0,0,0,0.02,0.05,0.000,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,5,15,0.00,0.00,0.00,20,NONE,1,2,0,0,0,0.00,0.00,0.000,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0,0,0.00,0.00,0.00,20,NONE,1,2,0,0,0,0.00,0.00,0.000,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0,0,0.00,0.00,0.00,1,30001,3,2,0.00000,0.00000,0.00,0.00,3,0,0,0,0,0.00,0.00,0,0,0,0,0,1,20001,DEEPSEA,2,0,0,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0,0,0,0.00,0.00,0.00,0,0,0,0.00,0.00,0.00,0.00,0.00,0,0.00,0.00,0,0,0,0,0.00,0.00,0.00,0.00,0.00,0,0.00,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,50,NONE,2,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0,1,497614,0,9552,911111972,0,6453691,0,0,0,11386038,8727133,0,0,0,0,0,228549
$BDEFGHOP,2.3,307,T139226500,1,060324130547,686,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0.0000,0.0000,0.0000,0.0000,0.0000,0.0000,0.0000,0.0000,0.00,0.00,37.00,30,1,16,1,0,1,0,1,0,0,0,0,0,0,0,1,0,0,0,5,20,NONE,1,2,0,0,0,0.00,0.00,0.000,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,1328,0,0.00,0.00,0.00,20,NONE,1,2,0,0,0,0.00,0.00,0.000,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0,0,0.00,0.00,0.00,20,NONE,1,2,0,0,0,0.02,0.05,0.000,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,5,15,0.00,0.00,0.00,20,NONE,1,2,0,0,0,0.00,0.00,0.000,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0,0,0.00,0.00,0.00,20,NONE,1,2,0,0,0,0.00,0.00,0.000,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0,0,0.00,0.00,0.00,1,30001,3,2,0.00000,0.00000,0.00,0.00,3,0,0,0,0,0.00,0.00,0,0,0,0,0,1,20001,DEEPSEA,2,0,0,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0,0,0,0.00,0.00,0.00,0,0,0,0.00,0.00,0.00,0.00,0.00,0,0.00,0.00,0,0,0,0,0.00,0.00,0.00,0.00,0.00,0,0.00,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,50,NONE,2,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0,1,497674,0,9552,911111972,0,6453751,0,0,0,11386098,8727133,0,0,0,0,0,228549
$BDEFGHOP,2.3,307,T139226500,1,060324130647,687,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0.0000,0.0000,0.0000,0.0000,0.0000,0.0000,0.0000,0.0000,0.00,0.00,38.00,30,1,16,1,0,1,0,1,0,0,0,0,0,0,0,1,0,0,0,5,20,NONE,1,2,0,0,0,0.00,0.00,0.000,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,1328,0,0.00,0.00,0.00,20,NONE,1,2,0,0,0,0.00,0.00,0.000,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0,0,0.00,0.00,0.00,20,NONE,1,2,0,0,0,0.02,0.05,0.000,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,5,15,0.00,0.00,0.00,20,NONE,1,2,0,0,0,0.00,0.00,0.000,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0,0,0.00,0.00,0.00,20,NONE,1,2,0,0,0,0.00,0.00,0.000,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0,0,0.00,0.00,0.00,1,30001,3,2,0.00000,0.00000,0.00,0.00,3,0,0,0,0,0.00,0.00,0,0,0,0,0,1,20001,DEEPSEA,2,0,0,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0,0,0,0.00,0.00,0.00,0,0,0,0.00,0.00,0.00,0.00,0.00,0,0.00,0.00,0,0,0,0,0.00,0.00,0.00,0.00,0.00,0,0.00,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,50,NONE,2,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0,1,497735,0,9552,911111972,0,6453811,0,0,0,11386158,8727133,0,0,0,0,0,228549
,1,060324130747,688,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0.0000,0.0000,0.0000,0.0000,0.0000,0.0000,0.0000,0.0000,0.00,0.00,38.00,30,1,16,1,0,1,0,1,0,0,0,0,0,0,0,1,0,0,0,5,20,NONE,1,2,0,0,0,0.00,0.00,0.000,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,1328,0,0.00,0.00,0.00,20,NONE,1,2,0,0,0,0.00,0.00,0.000,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0,0,0.00,0.00,0.00,20,NONE,1,2,0,0,0,0.02,0.05,0.000,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,5,15,0.00,0.00,0.00,20,NONE,1,2,0,0,0,0.00,0.00,0.000,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0,0,0.00,0.00,0.00,20,NONE,1,2,0,0,0,0.00,0.00,0.000,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0,0,0.00,0.00,0.00,1,30001,3,2,0.00000,0.00000,0.00,0.00,3,0,0,0,0,0.00,0.00,0,0,0,0,0,1,20001,DEEPSEA,2,0,0,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0,0,0,0.00,0.00,0.00,0,0,0,0.00,0.00,0.00,0.00,0.00,0,0.00,0.00,0,0,0,0,0.00,0.00,0.00,0.00,0.00,0,0.00,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,50,NONE,2,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0,1,497795,0,9552,911111972,0,6453871,0,0,0,11386218,8727133,0,0,0,0,0,228549
$BDEFGHOP,2.3,307,T139226500,1,060324130848,689,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0.0000,0.0000,0.0000,0.0000,0.0000,0.0000,0.0000,0.0000,0.00,0.00,38.00,30,1,16,1,0,1,0,1,0,0,0,0,0,0,0,1,0,0,0,5,20,NONE,1,2,0,0,0,0.00,0.00,0.000,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,1328,0,0.00,0.00,0.00,20,NONE,1,2,0,0,0,0.00,0.00,0.000,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0,0,0.00,0.00,0.00,20,NONE,1,2,0,0,0,0.02,0.05,0.000,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,5,15,0.00,0.00,0.00,20,NONE,1,2,0,0,0,0.00,0.00,0.000,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0,0,0.00,0.00,0.00,20,NONE,1,2,0,0,0,0.00,0.00,0.000,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0,0,0.00,0.00,0.00,1,30001,3,2,0.00000,0.00000,0.00,0.00,3,0,0,0,0,0.00,0.00,0,0,0,0,0,1,20001,DEEPSEA,2,0,0,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0,0,0,0.00,0.00,0.00,0,0,0,0.00,0.00,0.00,0.00,0.00,0,0.00,0.00,0,0,0,0,0.00,0.00,0.00,0.00,0.00,0,0.00,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,50,NONE,2,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0,1,497855,0,9552,911111972,0,6453932,0,0,0,11386278,8727133,0,0,0,0,0,228549
$BDEFGHOP,2.3,307,T139226500,1,060324130948,690,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0.0000,0.0000,0.0000,0.0000,0.0000,0.0000,0.0000,0.0000,0.00,0.00,38.00,30,1,16,1,0,1,0,1,0,0,0,0,0,0,0,1,0,0,0,5,20,NONE,1,2,0,0,0,0.00,0.00,0.000,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,1328,0,0.00,0.00,0.00,20,NONE,1,2,0,0,0,0.00,0.00,0.000,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0,0,0.00,0.00,0.00,20,NONE,1,2,0,0,0,0.02,0.05,0.000,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,5,15,0.00,0.00,0.00,20,NONE,1,2,0,0,0,0.00,0.00,0.000,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0,0,0.00,0.00,0.00,20,NONE,1,2,0,0,0,0.00,0.00,0.000,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0,0,0.00,0.00,0.00,1,30001,3,2,0.00000,0.00000,0.00,0.00,3,0,0,0,0,0.00,0.00,0,0,0,0,0,1,20001,DEEPSEA,2,0,0,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0,0,0,0.00,0.00,0.00,0,0,0,0.00,0.00,0.00,0.00,0.00,0,0.00,0.00,0,0,0,0,0.00,0.00,0.00,0.00,0.00,0,0.00,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,50,NONE,2,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0,1,497915,0,9552,911111972,0,6453992,0,0,0,11386339,8727133,0,0,0,0,0,228549

我尝试过的:

我尝试检查堆内存,也没有发生缓冲区溢出,因为数据在 2000 字节以内。

解决方案1

我编译了你的代码,只收到以下警告:

warning: suggest parentheses around assignment used as truth value [-Wparentheses]
    50 |         while (uPktlen = CustomGetLine(tempbuf, &PtrSize, pCurrFile, '$', u32Offset))

无论如何,它在我的 Linux 机器上运行良好(没有错误)。

你为什么打电话 perror 即使在 fgets 成功吗?

コメント

タイトルとURLをコピーしました