创建数组并从中检索数据时C ++中的未定义行为

时间:2019-03-14 10:50:44

标签: c++ undefined-behavior

我的行为不确定,并且由于我是C ++的初学者,所以我什至不知道如何尝试解决此问题。我正在查看this link,其中显示了可能的错误。我唯一的猜测是也许应该使用malloc?

#include <iostream>
using namespace std;

int main()
{

    int population[100];

    for (int i = 0; i < 100; ++i)
    {
        population[i] = rand()%(10);
    }

    int firstTournament[2];
    int secondTournament[2];

    int randomNumbers[4] = {};

    for (int i = 0; i < 4; ++i)
    {
        int indexOfIndividual = rand()%(101);
        randomNumbers[i] = indexOfIndividual;
    }

    for(int i = 0; i < 4; i++)
    {
        if(i < 2)
        {    
            firstTournament[i] = population[randomNumbers[i]];
            cout << "first tournament " << firstTournament[i] <<endl;
        }
        else
        {    
            secondTournament[i] = population[randomNumbers[i]];
            cout << "second tournament " << secondTournament[i] <<endl;
        }
    }

    for (int i = 0; i < 2; ++i)
    {
        // This is where the problem starts. It prints gibberish
        cout << "before tournament first  " << firstTournament[i] << endl;
        cout << "before tournament second " << secondTournament[i] << endl;
    }

    return 0;
}

这是打印出来的内容:

first tournament 4
first tournament 6
second tournament 5
second tournament 3

//This is where the fun begins:
before tournament first 4            // so far correct
before tournament second -1834234234 // ??? 
before tournament first 6            // correct
before tournament second 3266        // the numbers are supposed to be between 0 and 10

如何解决此问题?我究竟做错了什么?

我正在运行

g++ -std=c++11 -c -I /stuff/include main.cc

2 个答案:

答案 0 :(得分:1)

您的程序逻辑有故障。这是您填充secondTournament的地方:

for(int i = 0; i < 4; i++)
    {
        if(i < 2)
        {    
            firstTournament[i] = population[randomNumbers[i]];
            cout << "first tournament " << firstTournament[i] <<endl;
        }
        else
        {    
            secondTournament[i] = population[randomNumbers[i]];
            cout << "second tournament " << secondTournament[i] <<endl;
        }
    }

secondTournament[0]从未设置。因为当i0时,if(i < 2)true并且secondTournament[i] = ...不执行。然后,在此块中:

 for (int i = 0; i < 2; ++i)
    {
        // This is where the problem starts. It prints gibberish
        cout << "before tournament first  " << firstTournament[i] << endl;
        cout << "before tournament second " << secondTournament[i] << endl;
    }

您正在打印未设置的secondTournament[0]。相反,您要设置secondTournament[2]secondTournament[3],它们都超出范围。也许您想要的是secondTournament[i-2] = ...

答案 1 :(得分:1)

标准库具有许多功能,它们可以完全满足您在此处要尝试执行的操作。

#include <iostream>
#include <random>
#include <algorithm>

int main()
{
    std::mt19937 gen{std::random_device{}()}; // or any other prng   
    std::uniform_int_distribution<int> dis(0, 9);

    int population[100];
    std::generate(population, population + 100, [&]{ return dis(gen); });

    int firstTournament[2];
    int secondTournament[2];

    std::sample(population, population + 100, firstTournament, 2, gen);
    std::sample(population, population + 100, secondTournament, 2, gen);

    for (int i = 0; i < 2; ++i)
    {
        std::cout << "before tournament first  " << firstTournament[i] << std::endl;
        std::cout << "before tournament second " << secondTournament[i] << std::endl;
    }

    return 0;
}