从向量中删除取消引用的元素

发布于 01-06 05:12 字数 1786 浏览 1 评论 0原文

我使用 new 创建了对象,但在将它们添加到向量之前取消了它们的引用。尽管在互联网上搜寻,我仍无法弄清楚如何对这些项目调用删除。我想只使用标准 C++ 和 STL 来做到这一点,我不想(例如)使用 Boost 库。

正如你所看到的,a、b 和 c 失去了作用域,我留下了我认为是向量中的副本的内容。我该如何删除这些。我不想在数组中存储指针,因为我需要向 API 函数传递一个双精度数组。

请问有人 - 我如何删除这些对象?

#include <iostream>
#include <vector>

using namespace std;

vector<double> vectorDouble;
void createObjects();

void createObjects() {
    double* a=new double(13);
    double* b=new double(14);
    double* c=new double(15);
    //a,b and c are not contiguous memory blocks
    cout << "memory location of a: " << a << endl;
    cout << "memory location of b: " << b << endl;
    cout << "memory location of c: " << c << endl;

    vectorDouble.push_back(*a);
    vectorDouble.push_back(*b);
    vectorDouble.push_back(*c);
}

int main() {
    createObjects();
    //the memory addresses are contiguous 8 byte chunks
    cout << "vector memory at 0: " << &(vectorDouble[0]) << endl;
    cout << "vector memory at 1: " << &(vectorDouble[1]) << endl;
    cout << "vector memory at 2: " << &(vectorDouble[2]) << endl;

    //get pointer to the 2nd element
    double *P=&(vectorDouble[1]); 

    //dereference and look inside - two memory locations both contain the value 14
    cout << "vector Pointer P ["<< P <<"] contains " << *P <<endl;

    //Which should I call delete on? I have lost reference to the original pointers.
    //How should I call delete on the vector?

    cout << "deleting pointer that references 2nd vector element" << endl;
    delete P; //********* CRASH **********
    cout << "Done deleting" << endl;
}

I have created objects by using new, but then have dereferenced them before adding them to a vector. Despite trawling the internet I cannot work out how I can call delete on these items. I want to do this just using standard C++ and STL I don't want (e.g.) to use Boost libraries.

As you can see a, b and c lose scope and I am left with what I presume to be copies in the vector. How can I go about deleting these. I don't want to store pointers in the array as I will need to pass an API function an array of doubles.

Please someone - how do I delete these objects?

#include <iostream>
#include <vector>

using namespace std;

vector<double> vectorDouble;
void createObjects();

void createObjects() {
    double* a=new double(13);
    double* b=new double(14);
    double* c=new double(15);
    //a,b and c are not contiguous memory blocks
    cout << "memory location of a: " << a << endl;
    cout << "memory location of b: " << b << endl;
    cout << "memory location of c: " << c << endl;

    vectorDouble.push_back(*a);
    vectorDouble.push_back(*b);
    vectorDouble.push_back(*c);
}

int main() {
    createObjects();
    //the memory addresses are contiguous 8 byte chunks
    cout << "vector memory at 0: " << &(vectorDouble[0]) << endl;
    cout << "vector memory at 1: " << &(vectorDouble[1]) << endl;
    cout << "vector memory at 2: " << &(vectorDouble[2]) << endl;

    //get pointer to the 2nd element
    double *P=&(vectorDouble[1]); 

    //dereference and look inside - two memory locations both contain the value 14
    cout << "vector Pointer P ["<< P <<"] contains " << *P <<endl;

    //Which should I call delete on? I have lost reference to the original pointers.
    //How should I call delete on the vector?

    cout << "deleting pointer that references 2nd vector element" << endl;
    delete P; //********* CRASH **********
    cout << "Done deleting" << endl;
}

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

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

发布评论

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

评论(5

或十年2025-01-13 05:12:18

你需要删除的对象不在向量中,因为你的向量元素不是用new创建的。当您在 push_back 中使用取消引用运算符时,您只是将 malloc 的双精度值的副本传递给向量,而不是将实际分配的双精度值传递给向量。

您实际上只是泄漏了它们 - 您的代码无需分配双精度数即可正常运行:

void createObjects() {
    vectorDouble.push_back(13);
    vectorDouble.push_back(14);
    vectorDouble.push_back(15);
}

The objects you need to delete are not in the vector, because your vector elements are not created with new. You are just passing the copy of the malloc'd doubles, not the actual allocated double to the vector, when you use the dereference operator in the push_back.

You are actually just leaking them - your code would run fine without allocating doubles:

void createObjects() {
    vectorDouble.push_back(13);
    vectorDouble.push_back(14);
    vectorDouble.push_back(15);
}
原来是傀儡2025-01-13 05:12:18

正如每个人所指出的,没有任何理由在程序中调用new

void createObjects() {
    vectorDouble.push_back(13);
    vectorDouble.push_back(14);
    vectorDouble.push_back(15);
}

但是,假设您确实有某种理由调用new >。 (我无法想象它可能是什么,但我们假设你是一个天才)。这样做的方法如下:

void createObjects() {
    double* a=new double(13);
    double* b=new double(14);
    double* c=new double(15);
    //a,b and c are not contiguous memory blocks
    cout << "memory location of a: " << a << endl;
    cout << "memory location of b: " << b << endl;
    cout << "memory location of c: " << c << endl;

    vectorDouble.push_back(*a);
    vectorDouble.push_back(*b);
    vectorDouble.push_back(*c);

    delete a;
    delete b;
    delete c;
}

您看,push_back 不会将指针的副本放入向量中,而是将对象的副本放入向量中。一旦你复制了你的对象,那么你的对象的内存就不再有任何持续的用途,并且可以被销毁。

As everyone points out, there is no reason whatsoever to invoke new in your program:

void createObjects() {
    vectorDouble.push_back(13);
    vectorDouble.push_back(14);
    vectorDouble.push_back(15);
}

Suppose, however, that you do have some reason to call new. (I can't imagine what it might be, but let's assume you are a genius). Here is how you would do that:

void createObjects() {
    double* a=new double(13);
    double* b=new double(14);
    double* c=new double(15);
    //a,b and c are not contiguous memory blocks
    cout << "memory location of a: " << a << endl;
    cout << "memory location of b: " << b << endl;
    cout << "memory location of c: " << c << endl;

    vectorDouble.push_back(*a);
    vectorDouble.push_back(*b);
    vectorDouble.push_back(*c);

    delete a;
    delete b;
    delete c;
}

You see, the push_back doesn't put a copy of your pointer in the vector, it puts a copy of your object in the vector. Once you've made the copy of your object, then your object's memory serves no continuing purpose, and can be destroyed.

欲拥i2025-01-13 05:12:18

函数 createObjects 并未将分配的值放入向量中;它会放入值,然后泄漏abc 指向的内存。调用:

vectorDouble.push_back(*a);

a 指向的值存储在向量中(*a 取消引用指针,您可能已经知道了)。一旦该函数返回,指针就会丢失。您无法从向量中检索它们。您要么需要创建一个指向双精度的指针向量,要么(更有可能)甚至不分配值;只需存储双打即可。

The function createObjects is not putting the allocated values into the vector; it is putting in the values and then leaking the memory that a, b, and c pointed to. The call:

vectorDouble.push_back(*a);

stores the value pointed to by a in the vector (*a dereferences the pointer, which you probably already know). Once that function returns, the pointers are lost. You cannot retrieve them from the vector. You would either need to make a vector of pointers to doubles or (more likely) don't even allocate values; just store the doubles.

仅此而已2025-01-13 05:12:18

我认为我遇到了问题:

vectorDouble.push_back(*a);
vectorDouble.push_back(*b);
vectorDouble.push_back(*c);

您将 a、b 和 c 作为值传递,因此数组并不真正包含您创建的变量(这就是为什么它们现在具有不同的内存地址,它们是变量的内容!)。然后你可以删除方法内的变量,不要在方法内使用指针或使用 double* 向量。

I think I've got the problem, on:

vectorDouble.push_back(*a);
vectorDouble.push_back(*b);
vectorDouble.push_back(*c);

You are passing a, b and c as values so the array don't really contains the variables you have created (that's why they now have different memory address, they are a copy of the content of your variables!). Then you can delete the variables inside the method, don't use pointers inside the method or use an double* vector.

oО清风挽发oО2025-01-13 05:12:18

您的泄漏发生在 createObjects() 中,因为 std::vector<>::push_back() 复制了其参数。您应该在 createObjects() 作用域结束之前删除指针。

也就是说,我不明白为什么你一开始就使用动态分配。如果您可以避免这种情况,请这样做(您可以使用像 std::unique_ptr<> 这样的智能指针,或者更好的是使用普通的旧 double )。

Your leak is in createObjects(), because std::vector<>::push_back() makes a copy of its argument. You should delete the pointer before the end of scope of createObjects().

That said, I don't see why you use dynamic allocation to begin with. If you can avoid that please do (and you can, with a smart pointer like std::unique_ptr<>, or better yet with plain old doubles).

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