c++循环随机剪切

c++ Cycle cuts out randomly?

本文关键字:随机 循环 c++      更新时间:2023-10-16

首先我有这个类:

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;

        }
    }
}

基本上,此函数应该:-从子文件夹/配方读取文件名

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

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

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

问题是,由于某种原因,这个周期似乎是随机中断的。我想知道为什么,以及如何修复它:

示例输出:

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处理的是".txt",迭代2是另一个有效文件

我是个菜鸟,非常好,所以请友善一点:/使用CodeBlocks,在win64 上使用minGW

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

2) 您创建了一个大小为[word_count]-1=3的数组(即索引i范围为[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的析构函数中结束,试图删除指针。它们不仅悬挂着,而且每个都有几个副本。双重错误!