C++ c++;斯堪的纳维亚正在填满我的记忆

C++ c++;斯堪的纳维亚正在填满我的记忆,c++,file,ram,scandir,C++,File,Ram,Scandir,我使用在我的类中实现的函数: bool MyClass::getNextFile(string& s_first_file, const char* c_path_data){ //string s_first_file = ""; struct dirent **namelist; string s_file_actual = ""; int n; int i=0; n = scandir(c_path_data, &nameli

我使用在我的类中实现的函数:

bool MyClass::getNextFile(string& s_first_file, const char* c_path_data){
    //string s_first_file = "";
    struct dirent **namelist;
    string s_file_actual = "";
    int n;
    int i=0;
    n = scandir(c_path_data, &namelist, dataFilter, alphasort);
    if (n < 0){
        //perror("scandir");
        return false;
    }else{
        while (i<n) {
            s_file_actual = namelist[i++]->d_name;
            if(i==1){
                cout << "get file " << s_file_actual << "..." << endl;
                s_first_file = s_file_actual;
            }
        }
        free(namelist[i-1]);
        return true;
    }
}
bool MyClass::getNextFile(字符串&s\u第一个文件,常量字符*c\u路径数据){
//字符串s_first_file=“”;
结构目录**名称列表;
字符串s_file_actual=“”;
int n;
int i=0;
n=scandir(c_路径_数据、名称列表、数据过滤器、字母排序);
if(n<0){
//佩罗(“斯堪的纳维亚”);
返回false;
}否则{
while(id_name;
如果(i==1){
cout
while(id\u name;
如果(i==1){

难道你应该这样做吗

 while (i<n) 
 {
       s_file_actual = namelist[i++]->d_name;
       if (i==1)
       {
          cout << "get file " << s_file_actual << "..." << endl;
          s_first_file = s_file_actual;
       }
       free(namelist[i-1]); 
 }
 free(namelist);

    return true;
while(id\u name;
如果(i==1)
{

cout看起来您正在释放
namelist
的内容,但没有释放
namelist
本身。代码转储没有向我解释问题或修复=-1。
while (i<n) {
   s_file_actual = namelist[i++]->d_name;
   if (i==1) {
      cout << "get file " << s_file_actual << "..." << endl;
      s_first_file = s_file_actual;
   }
}
free(namelist[i-1]);
return true;
 while (i<n) 
 {
       s_file_actual = namelist[i++]->d_name;
       if (i==1)
       {
          cout << "get file " << s_file_actual << "..." << endl;
          s_first_file = s_file_actual;
       }
       free(namelist[i-1]); 
 }
 free(namelist);

    return true;