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

C++ 将fwrite()和fread()与malloc和realloc一起使用,c++,memory,malloc,fread,C++,Memory,Malloc,Fread,我的课程计划有问题。我会马上说,我并不是要求为我做这件事,,我只是想要更多关于如何使用这些功能的说明。看起来我使用它们的方式是正确的,因为它最多可以添加16个元素,但它挂起在fclose()或segfaults上 这就是我的工作。它是FAT16文件系统: directoryTable是结构的“数组” void writeDirectory(int FATTable[], directoryEntry* directoryTable) { int currentCluster = 1

我的课程计划有问题。我会马上说,我并不是要求为我做这件事,,我只是想要更多关于如何使用这些功能的说明。看起来我使用它们的方式是正确的,因为它最多可以添加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);
}
void writeDirectory(int-FATTable[],directoryEntry*directoryTable)
{   
int currentCluster=1;
directoryEntry*currentWrite=directoryTable;
FILE*theFile=fopen(fileSystemName,“rb+”);
如果(文件!=NULL)
{

cout当您使用realloc()目录时,它(可能)得到的内存空间(地址)与以前不同。那么,在realloc之后,currentRead指向哪里?(引导问题..)

该死,我以为这就是问题所在。但不,我只是在重新分配之前和之后对directoryTable指针运行了一个cout,它们是一样的。要回答你的问题,它仍然是指原始的directoryTable,它是malloc'd。如果不是“the”问题然后它仍然是问题之一。=)应该给它一个新的内存地址?这会破坏它吗?你是对的,伙计。我发现realloc只有在当前指针无法扩展时才会给一个新的内存地址,因为没有空间。这解释了为什么它只有在表变大时才会出现故障。一个新指针是rEturn和currentRead仍然指向旧的,因此它尝试读取到空闲内存中。
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;
}