gpt4 book ai didi

C++ - 删除包含 vector 的结构 vector 会导致内存泄漏吗?

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:31:56 24 4
gpt4 key购买 nike

我正在构建一个游戏,并且有一个称为 Sprite 的对象 vector 。

struct Sprite
{
SpriteType texture; // The texture enumeration
float x; // The x position of the sprite
float y; // The y position of the sprite
float xVel; // The x velocity of the sprite
float yVel; // The y velocity of the sprite
int imgBlockX; // The x block in the image
int imgBlockY; // The y block in the image
int numFrames; // The number of frames in the sprite animation
int curFrame; // The current frame of animation
int delay; // The delay between frame switches
int elapsed; // The amount of time on this frame
long lastTime; // The last update time
long curTime; // The current update time
bool loop; // Does this animation loop?
int lifespan; // The max lifespan of the sprite
int order; // 0 for first 1 for last
bool hasChildren; // Is this a parent sprite?
int maxSize;
std::vector<SpriteChild> children;// The sprites that are linked to this one (die when this one dies)
};

正如您在底部看到的那样,它本身包含一个子 Sprite 子 vector 。如果我从我的 sprite vector 中删除一个元素,它会导致 spritechild vector 发生内存泄漏还是会被处理?

谢谢!

最佳答案

你的 vector 被分配为 Sprite 结构的成员(它不是通过 new 分配的),所以当 Sprite 被删除时它会被自动清理。

当您通过 new 创建对象并且不delete 它时,您会遇到内存泄漏。

关于C++ - 删除包含 vector 的结构 vector 会导致内存泄漏吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11042780/

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