C++ 中的向量问题

发布于 2024-08-29 06:30:25 字数 1996 浏览 1 评论 0原文

我目前正在开发一个处理 People 类对象向量的项目。该程序编译并运行得很好,但是当我使用调试器时,当尝试对 PersonWrangler 对象执行任何操作时,它会死掉。我目前有 3 个不同的类,一个是针对人的,一个是负责处理所有人员的 personwrangler,还有一个是处理游戏输入和输出的游戏类。

编辑:我的基本问题是理解为什么它在调用outputPeople时会死掉。另外我想了解为什么我的程序完全按照预期工作,除非我使用调试器。 outputPeople 函数按照我的预期方式工作。

编辑2:调用堆栈有3个错误调用,它们是:

  1. std::vector >::begin(this=0xbaadf00d)
  2. std::vector >::size(this=0xbaadf00d)
  3. 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:

  1. std::vector >::begin(this=0xbaadf00d)
  2. std::vector >::size(this=0xbaadf00d)
  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

剑心龙吟 2024-09-05 06:30:25

0xBAADFOOD 是一个神奇数字,用于提醒您事实上,您正在处理未初始化的内存。从堆栈跟踪中,我们看到 PersonWrangler::outputPeople 中的 this 无效。因此,hal 并不指向有效的 PersonWrangler(也就是说,假设第 4 帧是对 Game::menu 的调用)。要自己解决此类问题,请从 Game::Game() 开始逐步执​​行代码,边检查 Game::hal 边查看可能发生的情况出问题了。

Game::Game 中,hal 是一个局部变量,阴影 Game::hal。当 Game::Game 退出时,此 hal 超出范围并泄漏内存,而 Game::hal 保持未初始化状态。您想要的是:

Game::Game()
{
    hal = new PersonWrangler(inputStartingNum);
}

调试器用幻数填充未初始化的内存,以便更容易发现错误。在生产版本中,内存中没有填充任何特定的内容;未初始化内存的内容未定义,并且可能保存有效值。这就是为什么生产构建可能不会失败而调试构建会失败。

0xBAADFOOD is a magic number to alert you to the fact that you're dealing with uninitialized memory. From the stack trace, we see that this in PersonWrangler::outputPeople is invalid. Thus hal doesn't point to a valid PersonWrangler (that is, assuming frame 4 is a call to Game::menu). To resolve this sort of thing yourself, step through the code, starting at Game::Game(), examining Game::hal as you go, to see what might be going wrong.

In Game::Game, hal is a local variable that shadows Game::hal. When Game::Game exits, this hal goes out of scope and leaks memory, while Game::hal remains uninitialized. What you want is:

Game::Game()
{
    hal = new PersonWrangler(inputStartingNum);
}

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.

半暖夏伤 2024-09-05 06:30:25

您是否初始化了 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 to delete your PersonWrangler somewhere, probably in the Game deconstructor.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文