gpt4 book ai didi

c++ - 在 C++ 中复制 vector 时 vector 运算符 = 出错

转载 作者:搜寻专家 更新时间:2023-10-31 01:46:15 25 4
gpt4 key购买 nike

在我的程序中,我正在复制 vector<vector<int> >对另一个这样的:

#include <vector>
#include <cstdlib>
#include <cmath>

typedef std::vector<std::vector<int> > VVector;

VVector mix_genome(VVector mix_genome,
VVector g1,
VVector g2,
int gene_length)
{
VVector gbuilt = g1; // valgrind gets angry at this a bit...

for(int i = 0; i < 30; i++)
{
int syngamete_chance = std::floor(std::rand() % 100);
if(syngamete_chance <= 50)
{
gbuilt[i] = g2[i];
}
}

int mutation_chance = floor(rand() % 100);

if(mutation_chance <= 3)
{
int gene_num = floor(rand() % 30);
int act_num = floor(rand() % gene_length+1);
int rand_act = floor(rand() % 8);

gbuilt[gene_num][act_num] = rand_act;
}

return gbuilt;
}

在运行程序(每隔一段时间调用此函数)大约 3 分钟后,会导致内存访问错误。 Valgrind 为我提供了有关此功能的以下信息:==31557== Invalid write of size 4Address 0x10207e360 is 0 bytes after a block of size 16 alloc'd

如果我禁用该函数并且不调用它,程序似乎不会崩溃。 GDB 给我 malloc: *** error for object 0x1068a6878: incorrect checksum for freed object - object was probably modified after being freed. .回溯显示它来自 operator=:

#13 0x000000010000f8d4 in std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >::operator= (this=0x7fff5fbfe780, __x=@0x7fff5fbfeb50) at vector.tcc:140

我认为这也会导致其他错误,但我不确定。

编辑:

这是我的 Tick()显然也是导致此错误的函数。

int Creature::Tick() {
if(!dead) {
food--;
timeLasted++;
step++;
if(step > maxStep) {
step = 0;
}
if(cooldown > 0) {
cooldown--;
}
if(xpos > 178) {
events[EVENT_RIGHT_SEEN_EDGE_OF_SCREEN] = true;
}
else if(xpos < 20) {
events[EVENT_LEFT_SEEN_EDGE_OF_SCREEN] = true;
}

if(ypos > 179) {
events[EVENT_BOTTOM_SEEN_EDGE_OF_SCREEN] = true;
}
else if(ypos < 20) {
events[EVENT_TOP_SEEN_EDGE_OF_SCREEN] = true;
}
events[EVENT_NOTHING_HAPPENED] = true;
for(int z = 0; z < NUM_EVENTS-1; z++) {
if(events[z]) {
events[EVENT_NOTHING_HAPPENED] = false; // events[z] is true so something happened
}
}
for(int i = 0; i < NUM_EVENTS-1; i++) { // last event should always be "nothing happened"
if(events[i]) {
Action(genome[i][step]); // this has been ided by valgrind: invalid read size 4
events[i] = false;
}
}
if(!fighting && events[EVENT_NOTHING_HAPPENED]) {
Action(genome[EVENT_NOTHING_HAPPENED][step]);
}
if(food < 0)
food = 0;
if(food > maxFood)
food = maxFood;
if(food == 0) {
lifetime -= 5;
}
if(xpos > 200-bodySize) {
xpos = 200-bodySize;
}
else if(xpos < 0) {
xpos = 0;
}
if(ypos > 200-bodySize) {
ypos = 200-bodySize;
}
else if(ypos < 0) {
ypos = 0;
}
}
return timeLasted;

最佳答案

实际上这个可能是问题所在:

int act_num = floor(rand() % gene_length+1);

你是说

int act_num = rand() % (gene_length+1);

?

这大致符合 valgrind 的最后提示:在 ~(207-188) == ~+19 行无效写入 mix_genome

这里是相关的部分:

==31557== Invalid write of size 4
==31557== at 0x100001C43: mix_genome(...) (Game.h:207)

...

==31557== Address 0x10207e360 is 0 bytes after a block of size 16 alloc'd
==31557== by 0x10000CB10: std::vector<...>::vector(std::vector<...> const&) (stl_vector.h:233)
==31557== by 0x100001AD3: mix_genome(std::vector<...>, ...) (Game.h:188)

旧答案文本

虽然目前还不知道这是否仍然相关,但最初的分析还展示了如何“读取”valgrind 诊断的其他方法:

您的问题不在于 vector 。看起来您有一个过时的引用(可能是线程错误)。

FPS: 0    # of Creatures: 414    # of Food: 348      ==31557== Invalid read of size 4
==31557== at 0x1000016BC: Creature::Tick() (Creature.h:204)

这告诉我,在 Tick() 中,您正在更新某种统计数据(包括打印在控制台上的 FPS?)。显然,这是指一个地址:

==31557==  Address 0x100095980 is 0 bytes after a block of size 16 alloc'd
==31557== at 0xC658: malloc (vg_replace_malloc.c:295)
...
(std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > > const&) (stl_vector.h:233)
==31557== by 0x10000CCA2: Creature::Creature(Creature const&) (Creature.h:52)

因此,它可能包含对已释放/重新分配的内容的引用。

请注意,当 vector 调整大小时,它们会使所有现有的引用、指针和/或迭代器失效。为了避免这种情况,

  • vector::reserve 可用于避免必须根据预期增长进行重新分配
  • 你可以使用 vector 索引
  • 你可以看看 boost::stable_vector

编辑 事实上,mix_genome 似乎也写入了无效地址。阅读更多您的 valgrind 日志。

关于c++ - 在 C++ 中复制 vector 时 vector 运算符 = 出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20927942/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com