C++ 中的向量问题
我目前正在开发一个处理 People 类对象向量的项目。该程序编译并运行得很好,但是当我使用调试器时,当尝试对 PersonWrangler 对象执行任何操作时,它会死掉。我目前有 3 个不同的类,一个是针对人的,一个是负责处理所有人员的 personwrangler,还有一个是处理游戏输入和输出的游戏类。
编辑:我的基本问题是理解为什么它在调用outputPeople时会死掉。另外我想了解为什么我的程序完全按照预期工作,除非我使用调试器。 outputPeople 函数按照我的预期方式工作。
编辑2:调用堆栈有3个错误调用,它们是:
- std::vector >::begin(this=0xbaadf00d)
- std::vector >::size(this=0xbaadf00d)
- PersonWrangler::outputPeople(this=0xbaadf00d)
相关代码:
class Game
{
public:
Game();
void gameLoop();
void menu();
void setStatus(bool inputStatus);
bool getStatus();
PersonWrangler* hal;
private:
bool status;
};
它调用了outputPeople,它立即因baadf00d错误而死掉。
void Game::menu()
{
hal->outputPeople();
}
其中 hal 是 PersonWrangler 类型的对象
class PersonWrangler
{
public:
PersonWrangler(int inputStartingNum);
void outputPeople();
vector<Person*> peopleVector;
vector<Person*>::iterator personIterator;
int totalPeople;
};
,outputPeople 函数定义为
void PersonWrangler::outputPeople()
{
int totalConnections = 0;
cout << " Total People:" << peopleVector.size() << endl;
for (unsigned int i = 0;i < peopleVector.size();i++)
{
sort(peopleVector[i]->connectionsVector.begin(),peopleVector[i]->connectionsVector.end());
peopleVector[i]->connectionsVector.erase( unique (peopleVector[i]->connectionsVector.begin(),peopleVector[i]->connectionsVector.end()),peopleVector[i]->connectionsVector.end());
peopleVector[i]->outputPerson();
totalConnections+=peopleVector[i]->connectionsVector.size();
}
cout << "Total connections:" << totalConnections/2 << endl;
}
Where hal is初始化
Game::Game()
{
PersonWrangler* hal = new PersonWrangler(inputStartingNum);
}
I am currently working on a project that deals with a vector of objects of a People class. The program compiles and runs just fine, but when I use the debugger it dies when trying to do anything with the PersonWrangler object. I currently have 3 different classes, one for the person, a personwrangler which handles all of the people collectively, and a game class that handles the game input and output.
Edit: My basic question is to understand why it is dying when it calls outputPeople. Also I would like to understand why my program works exactly as it should unless I use the debugger. The outputPeople function works the way I intended that way.
Edit 2: The callstack has 3 bad calls which are:
- std::vector >::begin(this=0xbaadf00d)
- std::vector >::size(this=0xbaadf00d)
- PersonWrangler::outputPeople(this=0xbaadf00d)
Relevant code:
class Game
{
public:
Game();
void gameLoop();
void menu();
void setStatus(bool inputStatus);
bool getStatus();
PersonWrangler* hal;
private:
bool status;
};
which calls outputPeople where it promptly dies from a baadf00d error.
void Game::menu()
{
hal->outputPeople();
}
where hal is an object of PersonWrangler type
class PersonWrangler
{
public:
PersonWrangler(int inputStartingNum);
void outputPeople();
vector<Person*> peopleVector;
vector<Person*>::iterator personIterator;
int totalPeople;
};
and the outputPeople function is defined as
void PersonWrangler::outputPeople()
{
int totalConnections = 0;
cout << " Total People:" << peopleVector.size() << endl;
for (unsigned int i = 0;i < peopleVector.size();i++)
{
sort(peopleVector[i]->connectionsVector.begin(),peopleVector[i]->connectionsVector.end());
peopleVector[i]->connectionsVector.erase( unique (peopleVector[i]->connectionsVector.begin(),peopleVector[i]->connectionsVector.end()),peopleVector[i]->connectionsVector.end());
peopleVector[i]->outputPerson();
totalConnections+=peopleVector[i]->connectionsVector.size();
}
cout << "Total connections:" << totalConnections/2 << endl;
}
Where hal is initialized
Game::Game()
{
PersonWrangler* hal = new PersonWrangler(inputStartingNum);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
0xBAADFOOD
是一个神奇数字,用于提醒您事实上,您正在处理未初始化的内存。从堆栈跟踪中,我们看到PersonWrangler::outputPeople
中的this
无效。因此,hal
并不指向有效的PersonWrangler
(也就是说,假设第 4 帧是对Game::menu
的调用)。要自己解决此类问题,请从Game::Game()
开始逐步执行代码,边检查Game::hal
边查看可能发生的情况出问题了。在
Game::Game
中,hal
是一个局部变量,阴影Game::hal
。当Game::Game
退出时,此hal
超出范围并泄漏内存,而Game::hal
保持未初始化状态。您想要的是:调试器用幻数填充未初始化的内存,以便更容易发现错误。在生产版本中,内存中没有填充任何特定的内容;未初始化内存的内容未定义,并且可能保存有效值。这就是为什么生产构建可能不会失败而调试构建会失败。
0xBAADFOOD
is a magic number to alert you to the fact that you're dealing with uninitialized memory. From the stack trace, we see thatthis
inPersonWrangler::outputPeople
is invalid. Thushal
doesn't point to a validPersonWrangler
(that is, assuming frame 4 is a call toGame::menu
). To resolve this sort of thing yourself, step through the code, starting atGame::Game()
, examiningGame::hal
as you go, to see what might be going wrong.In
Game::Game
,hal
is a local variable that shadowsGame::hal
. WhenGame::Game
exits, thishal
goes out of scope and leaks memory, whileGame::hal
remains uninitialized. What you want is:Debuggers fill uninitialized memory with magic numbers to make it easier to spot errors. In a production build, memory isn't filled with anything in particular; the content of uninitialized memory is undefined, and might hold valid values. This is why a production build might not fail when a debug build will.
您是否初始化了
hal
以指向实际的 PersonWrangler 对象?创建指针不会将其指向实际对象,除非您显式地这样做。您可能希望在构造时将 PersonWrangler 传递给游戏,或者让 Game 构造函数使用
new
创建 PersonWrangler。如果您选择后者,请确保在某处删除
您的 PersonWrangler,可能是在 Game 解构函数中。Did you initialize
hal
to point to an actual PersonWrangler object?Creating a pointer does not point it at an actual object unless you do it explicitly. You probably want to either pass a PersonWrangler to your Game at construction time, or have the Game constructor create a PersonWrangler using
new
. If you choose the latter, make sure todelete
your PersonWrangler somewhere, probably in the Game deconstructor.