我想了解这个指针错误。 [英] I want to understand this pointer error.

查看:120
本文介绍了我想了解这个指针错误。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码在这里。这几乎是1000行。前800行只是类和声明。问题始于910+线附近。



http://cpp.sh/7qrh [ ^ ]



我这里有这个代码,编译好了。

但是当它在第919行的向量上执行pop_back()时它会崩溃。

它崩溃并显示内存头文件(我不知道如何阅读) 。

错误是指针被释放未分配。



我还在学习,对调试知之甚少。



这是代码中的一些行。



The code is here. It's almost 1000 lines. The first 800 lines are just class and declaration. The problem start at around line 910+.

http://cpp.sh/7qrh[^]

I have this code here, it compiles ok.
But it crashes when executing pop_back() on the vector in line 919.
It crashes and show the memory header file (which I don't know how to read).
The error is 'pointer being freed was not allocated'.

I'm still learning and didn't know much about debugging.

This is some lines from the code.

template <typename A, typename B>
void battle(vector<A> &human, vector &zombie){

	while (!human.empty() || !zombie.empty()){
		human.pop_back();
	}
}





主要是我有这个。



In main, I have this.

vector<unique_ptr<Warrior>> warriors;
vector<unique_ptr<Knight>>  knights;
vector<unique_ptr<Goblin>>  goblins;
vector<unique_ptr<Orc>>     orcs;
battle(warriors, goblins);





我只想了解为什么会这样?

我该怎么做才能改善我的代码?



更新:

新代码: http: //cpp.sh/4pcg [ ^ ]

推荐答案

查看 pop_back()的定义。

Eg来自 http://www.cplusplus.com/reference/vector/vector/pop_back/ [< a href =http://www.cplusplus.com/reference/vector/vector/pop_back/target =_ blanktitle =New Window> ^ ]。它说来自空集合的 pop_back()未定义的行为,其中包括崩溃。





您的逻辑被破坏: while(!human.empty()||!zombie.empty())... 为空并且僵尸不为空,则code>也为真。在这种情况下你试图从人类弹出,即使它可能是空的。

删除僵尸部分,一切都很好。

[/编辑]



干杯

Andi
See the definition of pop_back().
E.g. from http://www.cplusplus.com/reference/vector/vector/pop_back/[^]. It says that pop_back() from an empty collection is undefined behavior, which includes "crashing".


Your logic is broken: while(!human.empty() || !zombie.empty()) ... is also true if human is empty and zombie is not empty. In that case you try to pop from human even it might be empty.
Remove the zombie part and all is fine.
[/EDIT]

Cheers
Andi


不知道。但是,我不打算去随机网站查看1000行代码,直到找到第910行,然后猜测哪一部分给你一个问题。



错误消息非常明确:未释放指针被释放 - 这意味着您没有为对象分配任何内存,或者您在那些1000中的某处损坏了指针代码行。



我无法运行它来查找,因为我不知道它做了什么,或者打算做什么,或者任何关于它的事情。即使我想,坦白说,我也不这样做。



所以忙着调试器并开始找出被释放的东西,它实际上有什么价值包含,然后开始寻找它应包含的内容,以及为什么不包含它。
No idea. But then, I'm not going to go to a random site and look at 1000 lines of code, counting them until I find "line 910" and then guessing which part of that is giving you a problem.

The error message is pretty explicit: "pointer being freed was not allocated" - which means that either you didn't allocate any memory for an object, or you corrupted the pointer somewhere in those 1000 lines of code.

And I can't run it to find out, because I have no idea what it does, or is meant to do, or anything about it. Even if I wanted to, which frankly, I don't.

So get busy with the debugger and start finding out what is being freed, and what value it actually contains, then start looking for what it should contain, and why it doesn't.


这篇关于我想了解这个指针错误。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆