c - 函数中返回文件/文件夹结构的指针

标签 c arrays winapi pointers struct

我正在尝试创建一个返回结构体指针数组的函数,但在返回数组时出现奇怪的行为。这是结构:

struct DLLs{
    char* FolderName = (char*)(malloc(sizeof(char*)*LongExtFileName));
    char* CppFileName = (char*)(malloc(sizeof(char*)*LongExtFileName));
};

这就是我的职责。此函数打开给定路径文件夹并返回其中包含的子文件夹。示例:“C:\”将返回 X 大小的数组,其中 array[0]->FolderName = "AMD", array[1] = "A_files"... 等根据 C 中包含的任何计算机和文件夹:*。

DLLs** GetFiles(char* DirPath, char* Extension){

    DLLs* arr[1]; //NULL struct in case there are wrong folders in given path
    arr[0] = (DLLs*)malloc(sizeof(DLLs*)*16);
    arr[0]->FolderName = "NULL";
    arr[0]->CppFileName = "NULL";

    char* path = (char*)(malloc(sizeof(char*)*LongExtFileName));
    path[0] = 0;

    strcat(path, DirPath);
    strcat(path, Extension); //This modifies the given path to search with WINAPI function
                             //path is changed to search files by: path\Extension, where Extension is a given extensión as \.cpp -> C:\*.cpp
                             //If extensions given by user is "\\*" it will search folders

    int NumFiles; //Number of folders found
    void* rootPath;
    WIN32_FIND_DATA FileInformation;


    if ((rootPath = FindFirstFile((path), &FileInformation)) == INVALID_HANDLE_VALUE)       
        return arr; //Wrong folder path given

    else NumFiles = 0;

    do {
        if(FileInformation.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY){
            NumFiles++;
        }

    } while (FindNextFile(rootPath, &FileInformation));

    FindClose(rootPath);

    if(!(NumFiles-2)) return arr; //If not folders found, skipping "." and ".." folders, return null struct.

    DLLs* array[NumFiles - 2]; //Create array of pointers of structs of number of found folders
    int i = -1;

    rootPath = FindFirstFile(path, &FileInformation);
    FindNextFile(rootPath, &FileInformation);
    FindNextFile(rootPath, &FileInformation); //Skip "." and ".." folders

    do {
        if(FileInformation.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY){
            array[i] = (DLLs*)malloc(sizeof(DLLs*)*256); //Allocate struct memory
            array[i]->FolderName = FileInformation.cFileName;
            printf("%s\n", array[i]->FolderName);
                    //When printing names in this process, it shows folders names correctly in console.

    }

    } while (++i, FindNextFile(rootPath, &FileInformation));

    FindClose(rootPath);

    //Weird behavior starts here:
    printf("\n\nFolders Saved:\n\n");
    for(i = 0; i<NumFiles-2; i++)
    printf("%s\n", array[i]->FolderName);  

    return array;   
};

当我打印找到文件夹时保存的结构信息时,它打印得很好,当文件夹查找周期结束并且我用for来一一显示结构数组信息时,数组是一团奇怪的乱七八糟的东西,有时会崩溃,有时它只是正确打印第一个文件夹名称,而其余的则带有奇怪的符号。

最佳答案

结构体定义中不允许有 =。无论如何,我不知道你想做什么。结构体定义如下:

struct DLLs
{
    char *FolderName;
    char *CppFileName;
};

稍后您可以声明这种类型的变量并使该变量的指针指向某处。

<小时/>

这是没有希望的:

DLLs* array[NumFiles - 2];
// ...
return array;

因为您要返回一个指向局部变量的指针。当函数返回时,array 被销毁。要使array 比其声明的函数持续时间更长,您必须将其设为静态,或使用动态分配。

即使你修复了这个问题,另一个错误是:

array[i]->FolderName = FileInformation.cFileName;

FileInformation 结构也是函数的本地结构,因此一旦函数退出,该指针将指向无效内存。

要解决此问题,您可以再次使用动态分配,但您必须保持一致并动态分配所有文件夹名称,以便稍后可以释放它们。

考虑事物在内存中的位置、它们将被分配多长时间以及指针指向何处,这一点非常重要。在一 block 泡尔上绘制内存映射可能会有所帮助,这样您就可以看到变量在哪里以及什么指向什么。

关于c - 函数中返回文件/文件夹结构的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29933605/

相关文章:

c - 在程序的任何地方单独分离一个线程是否正确?

c - 使 C 库线程安全

javascript - 创建键值 JavaScript 值

javascript - 使用 for 循环替换数组中的元素 javascript 未正确替换元素

javascript - 从二维数组中的每个数组中删除特定元素

python win32 COM关闭excel工作簿

c++ - 寻找使用 AgentX 实现 SNMP 表的示例代码

c - 为什么不同的测试有完全相同的数字?

c++ - 在 C/C++ 中创建新的证书存储

winapi - SHChangeNotify 与 SHCNE_RMDIR 行为在 Windows 7 和 Windows 10 之间不一致