为什么我可以访问刚刚从 c++ 中的 stl 向量中删除的元素?

发布于 2024-10-25 18:38:40 字数 763 浏览 1 评论 0原文

在此示例中,我创建一个包含一个整数的向量,然后从向量中删除该整数。向量的大小减小了,但整数仍然存在!为什么整数仍然存在?大小为 0 的向量怎么可能包含元素?

#include <vector>
#include <iostream>

using namespace std;

int main(int agrc, char* argv[])
{
    vector<int> v;
    v.push_back(450);

    cout << "Before" << endl;
    cout << "Size: " << v.size() << endl;
    cout << "First element: " << (*v.begin()) << endl;
    v.erase(v.begin());
    cout << "After" << endl;
    cout << "Size: " << v.size() << endl;
    cout << "First element: " << *(v.begin()) << endl;

    return(0);
}

输出:

Before
Size: 1
First element: 450
After
Size: 0
First element: 450

In this example, I create a vector with one integer in it and then I erase that integer from the vector. The size of the vector decreases, but the integer is still there! Why is the integer still there? How is it possible for a vector of size 0 to contain elements?

#include <vector>
#include <iostream>

using namespace std;

int main(int agrc, char* argv[])
{
    vector<int> v;
    v.push_back(450);

    cout << "Before" << endl;
    cout << "Size: " << v.size() << endl;
    cout << "First element: " << (*v.begin()) << endl;
    v.erase(v.begin());
    cout << "After" << endl;
    cout << "Size: " << v.size() << endl;
    cout << "First element: " << *(v.begin()) << endl;

    return(0);
}

output:

Before
Size: 1
First element: 450
After
Size: 0
First element: 450

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

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

发布评论

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

评论(5

濫情▎り 2024-11-01 18:38:40

您通过取消引用无效的内存位置来调用未定义的行为。通常,为了提高效率,堆管理器不会立即释放使用delete删除的内存。但是,这并不意味着您可以访问该内存位置,堆管理器可以随时将此内存位置用于其他目的。因此,如果您取消引用无效的内存位置,您的程序将出现不可预测的行为。

You are invoking undefined behavior by dereferencing an invalid memory location. Normally, the heap manager will not immediately free the memory deleted using delete for efficiency purposes. However, that doesn't mean that you can access that memory location, heap manager can use this memory location for other purposes whenever it likes. So your program will behave unpredictably if you dereference a invalid memory location.

叹倦 2024-11-01 18:38:40

IIRC 除非特别告知,否则向量不会释放空间,因此您会看到一个仍在其内存中但未被向量跟踪的项目。这是您应该首先检查大小的部分原因(另一个原因是,如果您从未分配过任何内容,则将取消引用垃圾指针)。

IIRC a vector doesn't release space unless specifically told to, so you're seeing an item which is still in its memory but not being tracked by the vector. This is part of the reason why you're supposed to check the size first (the other being that if you never assigned anything, you'll be dereferencing a garbage pointer).

陌若浮生 2024-11-01 18:38:40

首先,不要指望所有系统都会如此。向量内部的工作方式完全依赖于实现。通过取消引用无效的内存位置,您可以规避文档中概述的行为。
也就是说,您只能依赖 STL 文档中概述的行为。

您仍然可以访问该内存位置的原因是因为您正在使用的特定实现不会立即删除内存,而是将其保留一段时间(可能是出于性能目的)。如果作者愿意,另一种实现很可能立即删除该内存。

To start, don't count on it being this way across all systems. How a vector works internally is completely implementation-dependent. By dereferencing an invalid memory location, you're circumventing the behavior that has been outlined in the documentation.
That is to say, you can only count on behavior working that is outlined in the STL docs.

The reason you can still access that memory location is because that particular implementation you are using doesn't immediately delete memory, but keeps it around for awhile(probably for performance purposes). Another implementation could very well delete that memory immediately if the author so desired.

ゝ杯具 2024-11-01 18:38:40

只是向量没有释放内存,而是保留它以供将来使用。

这就是我们所说的“未定义行为”。不能保证它下次会起作用,并且在将来的尝试中很容易使程序崩溃。不要这样做。

It is just that the vector has not freed the memory, but kept it around for future use.

This is what we call "undefined behaviour" There is no guarantee that it will work next time and it may easily crash the program on a future attempt. Don't do it.

冬天旳寂寞 2024-11-01 18:38:40

你的编译器选项是什么?我和平常一样崩溃了
选项,以及我经常使用的两个编译器(g++ 和
VC++)。对于 g++,您必须设置一些额外的
此行为的选项(-D_GLIBCXX_DEBUG,我认为);据,直到...为止
我可以看出,这是 VC++ 的默认设置。 (我的 VC++ 命令是
只是“cl /EHsbounds.cc”。)

正如其他人所说,这是未定义的行为,但具有良好的
编译器,它会被定义为导致程序崩溃。

What are your compiler options? I get a crash with the usual
options, with both of the compilers I regularly use (g++ and
VC++). In the case of g++, you have to set some additional
options (-D_GLIBCXX_DEBUG, I think) for this behavior; as far as
I can tell, it's the default for VC++. (My command for VC++ was
just "cl /EHs bounds.cc".)

As others have said, it's undefined behavior, but with a good
compiler, it will be defined to cause the program to crash.

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