创建数组并从中检索数据时 C++ 中的未定义行为
Undefined behaviour in C++ while creating arrays and retrieving data from them
我有未定义的行为,因为我是 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
你的程序逻辑有问题。这是您填充 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]
从未设置。因为当 i
是 0
时, 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] = ...
.
标准库中有许多函数可以完全满足您的要求。
#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;
}
我有未定义的行为,因为我是 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
你的程序逻辑有问题。这是您填充 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]
从未设置。因为当 i
是 0
时, 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] = ...
.
标准库中有许多函数可以完全满足您的要求。
#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;
}