2016-06-19 4 views
-1

これらのコードは機能しません! ファイルの最後まで512バイトのブロックを読み込む必要があります。*** `./recover 'のエラー:free():無効な次のサイズ(通常)

  • valgrindはすべてok!フリー():無効次サイズ(通常):* 0x09e89170 中止(コアダンプ)

    #include <stdio.h> 
    #include <stdlib.h> 
    #include <stdint.h> 
    
    #define B_SIZE 512 
    
    char* getTitle (int c); 
    
    int main(int argc, char* argv[]) 
    { 
        // TODO 
    
        long size; 
        uint32_t *data; 
    
        // open file 
    
        FILE* file = fopen("card.raw", "r"); 
    
        if (!file) { 
         fprintf(stderr, "Unable to open/create file\n"); 
         return 1; 
        } 
    
        fseek(file, 0, SEEK_END); 
        size = ftell(file); 
        fseek(file, 0, SEEK_SET); 
    
    
        if (!(data = malloc(512))) { 
         fprintf(stderr, "Failed to allocate memory\n"); 
         return 1; 
        } 
    
        while(true) // until end 
        { 
         // read 512 block 
         if (ftell(file) >= size-2048) 
         { 
          printf("STOP\n"); 
          break; 
         } 
    
         fread(data, B_SIZE, 128, file); 
    
         printf("%ld, (%li)\n", ftell(file), size); 
    
        } 
    
        // close all files 
        free(data); 
        fclose(file); 
        return 0; 
    } 
    
    データ*端

で `./recover 'の誤差を解放さ割り当て

  • 答えて

    3

    B_SIZE * 128(512 * 128 = 64k)バイトを512バイトのバッファに読み込みます。それは割り当てられたメモリの境界から書き出し、の定義されていない動作につながります。

    一度に512バイトしか読み取らない場合は、たとえば512バイトを読み取ります。

    関連する問題