2012-02-18 38 views
0

私のクラスでプロジェクトに問題があります。 私はそれが私のために行われることを求めていません、私はちょうどこれらの機能の使用方法についてのより明確な説明をしたいと思います。 16個の要素を追加するまで動作するので、正しい方法で使用しているようですが、fclose()またはsegfaultsにハングします。mallocとreallocでのfwrite()とfread()の使用

ここで私は何をしているのですか。これはFAT16ファイルシステムです。

directoryTableは構造体の「配列」です。

void writeDirectory(int FATTable[], directoryEntry* directoryTable) 
{ 
    int currentCluster = 1; 
    directoryEntry* currentWrite = directoryTable; 

    FILE* theFile = fopen (fileSystemName, "rb+"); 
    if(theFile != NULL) 
    { 
     cout << fseek (theFile, (clusterSize * currentCluster), SEEK_SET) << endl; 
     cout << fwrite(currentWrite, clusterSize, 1, theFile) << endl ; 

     while(FATTable[currentCluster] != 0xFFFF) 
     { 
      currentWrite = currentWrite + numberOfEntries; 
      currentCluster = FATTable[currentCluster]; 
      cout << fseek (theFile, (clusterSize * currentCluster), SEEK_SET) << endl; 
      cout << fwrite(currentWrite, clusterSize, 1, theFile) << endl; 
     } 

     fflush(theFile); 
     cout << "Closing.." << errno << endl; 
     fclose(theFile); 
    } 
    else 
     cout << "FILE COULDN'T OPEN." << endl; 

    //Clean up that pointer 
    free(directoryTable); 
} 

基本的にポインタ(カレント書き込みは)別のポインタ(directoryTable)の先頭から開始し、ファイルの部分に一度バイト1のclusterSizeを書き込みます。 array/pointer(directoryTable)がそれ以上残っている場合は、currentWriteを上にしてclusterSizeバイトをインクリメントし、再度書き込みます。

それから、これを使用して、配列/ポインタを読んで構築します:

directoryEntry* readDirectory(int FATTable[]) 
{ 
    int currentCluster = directoryIndex; 

    //Allocate a clusterSize of memory 
    directoryEntry* directoryTable; 
    directoryTable = (directoryEntry*) malloc(clusterSize); 

    if(directoryTable == NULL) 
     cout << "!!! ERROR: Not enough memory!" << endl; 

    //A pointer to a part of that array 
    directoryEntry* currentRead = directoryTable; 
    numberOfDirTables = 1; 

    FILE* theFile = fopen (fileSystemName, "rb+"); 
    if(theFile != NULL) 
    { 
     //Seek to a particular cluster in the file (
     cout << fseek (theFile, (clusterSize * currentCluster), SEEK_SET) << endl; 
     cout << fread(currentRead, clusterSize, 1, theFile) << endl; 

     while(FATTable[currentCluster] != 0xFFFF) 
     { 
      numberOfDirTables++; 
      currentCluster = FATTable[currentCluster]; 
      directoryTable = (directoryEntry*) realloc(directoryTable, (clusterSize*numberOfDirTables)); 
      if(directoryTable == NULL) 
       cout << "!!! ERROR: Not enough memory!" << endl; 
      currentRead = currentRead + numberOfEntries; 
      cout << fseek (theFile, (clusterSize * currentCluster), SEEK_SET) << endl; 

      cout << fread(currentRead, clusterSize, 1, theFile) << endl; 
     } 
     cout << "Closing..." << errno << endl; 
     fclose(theFile); 
     cout << "Closed." << endl; 
    } 
    else 
     cout << "FILE COULDN'T OPEN." << endl; 
    return directoryTable; 
} 

基本的には最初のクラスタから読み込み、配列にそれを配置します。次に、currentRead clusterSizeバイト数を増やして、別のクラスタから読み込みます。しかし、それはreallocの最初ですので、配列は別のclusterSizeバイトに展開されます。

私はfwrite、fread、malloc、reallocを正しく使用していますか?それはある時点まで機能しているので、そうだと思われます。しかし、私はC++であまり強くないので、私は大きなメモリの問題を引き起こしている小さなものがないと思っています。

また、私は構造体の配列を構築しているので、代わりにcallocを使用する必要がありますか?

答えて

1

realloc()directoryTableを実行すると(おそらく)以前とは異なるメモリスペース(アドレス)が取得されます。だから、reallocの後、currentRead pointはどこですか? (先の質問..)

+0

私はそれが問題だと思った。しかし、いいえ、私はちょうど、再配置の前後でdirectoryTableポインタを実行したが、それらは同じである。あなたの質問に答えるために、それはまだmallocされた元のディレクトリテーブルを参照します。 –

+0

それは "それ"の問題ではなかった場合は、それはまだ問題の一つです。 =) –

+0

新しいメモリアドレスが与えられるはずですか?それはそれを壊すだろうか? –

関連する問題