将fwrite()和fread()与malloc和realloc一起使用

时间:2012-02-18 22:32:33

标签: c++ memory malloc fread

我的课程项目遇到了问题。我马上就说明了我不是要求它为我完成,我只想更多地澄清如何使用这些功能。似乎我正在以正确的方式使用它们,因为它可以添加16个元素,但它会挂起在fclose()或segfaults上。

这是我正在使用的内容。这是一个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);
}

基本上,指针(currentWrite)从另一个指针(directoryTable)的开头开始,并将一个字节的clusterSize一次写入文件的某些部分。如果剩下更多的数组/指针(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字节数并从另一个集群读取。但它首先重新分配,因此数组扩展了另一个clusterSize字节。

所以我的问题是,我正确使用fwrite,fread,malloc和realloc吗?它会显得如此,因为它可以工作到某一点。但是我在C ++方面不是太强大,所以我想我错过了一个导致重大内存问题的小事。

另外,我应该使用calloc,因为我正在构建一个结构数组吗?

1 个答案:

答案 0 :(得分:1)

当你realloc()directoryTable时,它(可能)获得的内存空间(地址)与以前不同。那么,在realloc之后,currentRead指向哪里? (主要问题..)