c ++ Cycle随机切断?

c++ Cycle cuts out randomly?

首先我有这个 class:

class Recept
{
    private:
       int serves;
       string* ingredient_name;
       int* ingredient_number;
       float difficulty;

    public:
       Recept(int a=0, string* b = NULL, float c = 0.0, int* d = NULL)
       {
          serves = a;
          ingredient_name = b;
          difficulty = c;
          ingredient_number = d;

       }

    ~Recept()
    {
        delete ingredient_name;
        delete ingredient_number;
    }
};

存储所有可用食谱的对象:

Recept* AvailableRecipes;

并用这个函数来初始化这个对象。 main() 唯一做的就是调用这个函数。

void OpenRecipes()
{

    SetCurrentDirectory("\Recipes");
    system("dir /b > a.txt");

    ifstream filelist;
    filelist.open("a.txt");
    stringstream newstrstr;
    newstrstr << filelist.rdbuf();
    string seged = newstrstr.str();

    filelist.clear();
    filelist.seekg(0, ios::beg);

    newstrstr.str(std::string());
    AvailableRecipes = new Recept[count_words(seged)-1];

    string filename;
    int counter = 0;

    cout << "Total number of iterations needed: " << count_words(seged) << endl;

    for(int i = 0; i < count_words(seged) ; i++)
    {
        cout << "i: " << i << endl;
        filelist >> filename;

        if(filename != "a.txt")
        {
            stringstream newstrstr;
            ifstream input;

            input.open(filename.c_str());
            newstrstr << input.rdbuf();


            string seged2 = newstrstr.str();
            int ingredient_num[(count_words(seged2) - 2) / 2];
            string ingredient_name[(count_words(seged2) - 2) / 2];
            float difficulty;
            int serving;
            input.clear();
            input.seekg(0, ios::beg);

            input >> serving >> difficulty;

            int IntContain;
            string StringContain;

            for (int j = 0; j < sizeof(ingredient_num)/sizeof(ingredient_num[0]); j++)
            {
                input >> IntContain >> StringContain;
                ingredient_num[j] = IntContain;
                ingredient_name[j] = StringContain;


           }

            Recept a = Recept(serving, ingredient_name, difficulty, ingredient_num);

            AvailableRecipes[counter] = a;

           counter++;


            newstrstr.str(std::string());

            input.close();

            cout << "No error so far" << endl;


        }

    }

}

基本上这个函数应该: - 从子文件夹 /Recipes

中读取文件名

-将文件名存储在同一文件夹中 "a.txt"。

-逐一打开文件,并根据其中的文本创建 Recipe 对象。

-将 Recipe 对象添加到 AvailableRecipes 对象数组。

问题是,出于某种原因,循环看似随机中断。我想知道为什么,以及如何解决它:s

示例输出:

Total number of iterations needed: 4
i: 0
No error so far
i: 1
i: 2

Process returned -1073741819 (0xC0000005)   execution time : 1.312 s
Press any key to continue.

//在本例中,迭代 0 使用有效文件 (!="a.txt) ,迭代 1 正在处理“a.txt,迭代 2 是另一个有效文件。

我是菜鸟,非常菜鸟,所以请多关照:/ 在 win64

上使用 CodeBlocks 和 minGW

这根本不是随机的。根据您发布的代码的理解: 1) 数组中需要存储4个单词。

2) 您创建了一个大小为 [word_count] - 1 = 3 的数组(即索引范围为 [0..2])。

3) 您的 for 循环有 4 次迭代(与 [word_count] 相同)(即遍历 [0..3] 范围内的索引)。

4) 当您尝试访问索引为 3 的数组元素时,会出现访问冲突错误,因为它不存在(请记住:您的数组的最大索引为 2 ).这就是异常代码 0xC0000005 的含义 - 访问冲突。

这两行已经有两个问题

        Recept a = Recept(serving, ingredient_name, difficulty, ingredient_num);

        AvailableRecipes[counter] = a;

首先创建 a,它存储指向局部变量 ingredient_nameingredient_num 的指针。一旦您离开声明它们的 if 语句,这些指针就会悬空。

然后在下一行中,您在 AvailableRecipes 数组中创建 a 的副本。现在每个指针都有两个副本。

最终,所有这些都将在 Recept 的析构函数中结束,试图删除指针。它们不仅悬空,而且每个都有多个副本。双误!