我有一个外部C-DLL,可在我的C ++项目中使用。
我坚持的功能是
Get_ALLFiles(char*** listOfFiles, int* nbrOfFiles)
。此功能在文件夹上应用一些条件,并返回与条件匹配的文件。
int nbrOfFiles= 0;
//just get the number of files
Get_ALLFiles((char***)malloc(1 * sizeof(char***)), &ElementNbr);
// pointer allocation
char ***MyFilesList = (char***)malloc(nbrOfFiles* sizeof(char**));
for (int i = 0; i < ElementNbr; i++) {
MyFilesList [i] = (char**)malloc(ElementNbr * 32 * sizeof(char*));
for (int j = 0; j < 32; j++)
MyFilesList [i][j] = (char*)malloc(ElementNbr * sizeof(char));
}
//Now i will use the function in order to get all the files (in my exemple
//I have 10 which respond the criteria
Get_ALLFiles(MyFilesList , &nbrOfFiles);
在“ MyFilesList”中,我只有第一个元素,如何获得“ MyFilesList”中的所有元素?
答案 0 :(得分:0)
您应该将变量的地址传递给函数,而不是传递给动态内存的指针。
也就是说,就像处理数字一样。
该函数将分配所有内存,并通过收到的指针更新变量。
赞:
char** MyFilesList = nullptr;
int nbrOfFiles = 0;
Get_ALLFiles(&MyFilesList , &nbrOfFiles);
答案 1 :(得分:0)
我的 猜测 是该函数自己分配内存,您应该将指针传递给接收值的变量。在C中模拟传递引用。
类似
char** MyFilesList;
int NumberFiles;
// Get a list of all files
Get_ALLFiles(&MyFilesList, &NumberFiles);
// Print all files
for (int i = 0; i < NumberFiles; ++i)
{
std::cout << "File #" i + 1 << " is " << MyFilesList[i] << '\n';
}
// Free the memory
for (int i = 0; i < NumberFiles; ++i)
{
free(MyFilesList[i]);
}
free(MyFilesList);