[ad_1]
Tôi đã viết một chương trình c đơn giản như bên dưới, nơi chúng tôi đang cố đọc dữ liệu từng dòng bằng cách sử dụng fgets() và chúng tôi bỏ qua các dòng không bắt đầu bằng ký tự ‘$’, vấn đề tôi gặp phải là tôi gặp lỗi là fgets: Không thể phân bổ bộ nhớ ở dòng thứ 4 và nó xảy ra ngẫu nhiên
#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; }
Dữ liệu tệp là:
$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
Những gì tôi đã thử:
Tôi đã thử kiểm tra bộ nhớ heap, lỗi tràn bộ đệm cũng không xảy ra do dữ liệu nằm trong phạm vi 2000 byte.
Giải pháp 3
Bạn có một mối nguy hiểm tiềm ẩn trong CustomGetLine
chức năng trong các dòng sau:
if (dPtr[0] != cStartChar) { // Skip this line by reading the next one return CustomGetLine(dPtr, n, stream, cStartChar, offset); }
Một hàm liên tục gọi chính nó được gọi là đệ quy và có thể dẫn đến sự cố nếu dữ liệu lớn.
Giải pháp 1
Tôi đã biên soạn mã của bạn và chỉ nhận được cảnh báo sau:
warning: suggest parentheses around assignment used as truth value [-Wparentheses] 50 | while (uPktlen = CustomGetLine(tempbuf, &PtrSize, pCurrFile, '$', u32Offset))
dù sao thì nó vẫn chạy tốt (không có lỗi) trên hộp Linux của tôi.
Tại sao bạn gọi perror
ngay cả trên fgets
thành công?
Giải pháp 2
đã biên dịch mã của bạn trên Windows và không tìm thấy vấn đề cấp phát bộ nhớ nào. fgets không phân bổ bộ nhớ bổ sung mà chỉ sao chép nội dung từ luồng tệp vào con trỏ nguồn. Vì bạn đã phân bổ 5000 byte bộ nhớ bằng malloc và luôn sử dụng lại dung lượng, do đó, nó sẽ không tạo ra bất kỳ vấn đề bộ nhớ nào.
Kiểm tra môi trường của bạn, vấn đề dường như nằm ở môi trường của bạn. Mã của bạn đang chạy ổn trong cả Linux, Windows và Trực tuyến.
[ad_2]
コメント