[ad_1]
He escrito un programa en C simple como se muestra a continuación, donde intentamos leer datos línea por línea usando fgets() y omitimos las líneas que no comienzan con el carácter ‘$’, el problema al que me enfrento es que estoy obteniendo un error como fgets: no se puede asignar memoria en la cuarta línea y sucede de forma aleatoria
#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; }
Los datos del archivo son:
$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
Lo que he probado:
Intenté verificar la memoria del montón y tampoco se produce un desbordamiento del búfer ya que los datos están dentro de los 2000 bytes.
Solución 3
Tiene un peligro potencial en su CustomGetLine
función, en las siguientes líneas:
if (dPtr[0] != cStartChar) { // Skip this line by reading the next one return CustomGetLine(dPtr, n, stream, cStartChar, offset); }
Una función que se llama a sí misma repetidamente se conoce como recursividad y podría generar problemas si los datos son grandes.
Solución 1
Compilé su código y recibí solo la siguiente advertencia:
warning: suggest parentheses around assignment used as truth value [-Wparentheses] 50 | while (uPktlen = CustomGetLine(tempbuf, &PtrSize, pCurrFile, '$', u32Offset))
de todos modos funciona bien (sin errores) en mi máquina Linux.
¿Por qué llamas? perror
incluso en fgets
éxito?
Solución 2
Compilé su código en Windows y no se encontraron problemas de asignación de memoria. fgets no asigna memoria adicional, sino que simplemente copia el contenido de la secuencia del archivo al puntero de origen. Dado que ya ha asignado 5000 bytes de memoria usando malloc y reutilizando el espacio todo el tiempo, no debería crear ningún problema de memoria.
Verifique su entorno, el problema parece estar en su entorno. Su código funciona bien tanto en Linux, Windows como en línea.
[ad_2]
コメント