std::vector 的奇怪行为

发布于 2024-09-02 21:45:10 字数 384 浏览 5 评论 0原文

我有一个这样的类:

class OBJ{...};

class A
{
   public:
   vector<OBJ> v;
   A(int SZ){v.clear(); v.reserve(SZ);}
};

A *a = new A(123);
OBJ something;
a->v.push_back(something);

这是我的代码的简化版本。 问题是在调试模式下它工作得很好。 但在发布模式下,它会在“push_back”行崩溃。 (所有优化标志均关闭) 我在release模式下调试了一下,问题出在A的构造函数上。 向量的大小对于虚拟值来说非常大,当我清除它时,它不会改变......

你知道为什么吗?

谢谢,

I have a class like this:

class OBJ{...};

class A
{
   public:
   vector<OBJ> v;
   A(int SZ){v.clear(); v.reserve(SZ);}
};

A *a = new A(123);
OBJ something;
a->v.push_back(something);

This is a simplified version of my code.
The problem is in debug mode it works perfect.
But in release mode it crashes at "push_back" line. (with all optimization flags OFF)
I debugged it in release mode and the problem is in the constructor of A.
the size of the vector is something really big with dummy values and when I clear it, it doesn't change...

Do you know why?

Thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

黄昏下泛黄的笔记 2024-09-09 21:45:11

我可以猜测 - 我想说 OBJ 可能没有正确实现的复制构造函数和/或赋值运算符和析构函数。

I can guess - I would say that OBJ probably does not have a correctly implemented copy constructor and/or assignment operator and destructor.

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