Impossible d’allouer de la mémoire : fgetc ou fgets

la programmation

[ad_1]

J’ai écrit un simple programme C comme ci-dessous dans lequel nous essayons de lire les données ligne par ligne en utilisant fgets() et nous sautant les lignes qui ne commencent pas par le caractère ‘$’, le problème auquel je suis confronté est que je suis obtenir une erreur en tant que fgets : impossible d’allouer de la mémoire à la 4ème ligne et cela se produit de manière aléatoire

#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;
}

Les données du fichier sont :

$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

Ce que j’ai essayé :

J’ai essayé de vérifier la mémoire du tas, le débordement de tampon ne se produit pas non plus puisque les données se trouvent dans les 2 000 octets.

Solution 1

J’ai compilé votre code, obtenant juste l’avertissement suivant :

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

de toute façon, cela fonctionne bien (pas d’erreur) sur ma machine Linux.

Pourquoi appelles-tu perror même sur fgets succès ?

[ad_2]

コメント

Titre et URL copiés