从向量中删除取消引用的元素 [英] Deleting dereferenced elements from vector

查看:159
本文介绍了从向量中删除取消引用的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过使用new创建了对象,但是在将它们添加到向量之前已经取消引用它们。尽管拖网网络,我不能解决我如何可以调用删除这些项目。我想使用标准的C ++和STL我不想(例如)使用Boost库。

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.

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

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;
}


推荐答案

/ em>指出,在您的程序中没有任何理由调用 new

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);
}

然而,假设你有一些理由调用 new 。 (我不能想象它可能是什么,但让我们假设你是一个天才)。下面是你如何做的:

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;
}

你会看到, push_back 不会将指针的副本放在向量中,它会将对象的副本放在向量中。一旦你创建了对象的副本,那么对象的内存不会继续存在,并且可以被销毁。

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.

这篇关于从向量中删除取消引用的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆