删除由另一个函数返回的指针指向的对象
Delete an object pointed by a pointer returned from another function
一个例子,
#include<iostream>
using namespace std;
vector<int>* F()
{
vector<int>*x=new vector<int>(3);
x[1]=1; x[2]=2; x[3]=3;
return x;
}
现在我想删除向量或释放另一个函数F1中x指向的内存块,类似于:
bool F1(vector<int>*x)
{
delete x;
return 1;
}
有什么可以实现我想要的吗?
谢谢!
使您的F()
功能类似
#include<iostream>
using namespace std;
vector<int> F() {
vector<int> x;
x.resize(4);
x[1]=1; x[2]=2; x[3]=3;
return x;
}
不需要F1()
。
相关文章:
- 为什么指针对象没有调用默认构造函数
- 如何访问双指针对象中的方法
- 如何使用智能指针对象执行成员函数指针
- 调用带有指针对象错误的指针变量
- 如何从模板类重载创建的指针对象上的运算符?
- C++指针对象的运算符重载
- 如何初始化数组指针对象
- 如何打印出唯一指针对象向量中的值(基于范围的循环)?C++
- C++指针对象差异
- 涉及继承,指针对象的交易以及何时进行的问题
- 表达式必须在C 中具有指针对象类型
- 下标需要数组或指针类型表达式必须具有指针对象类型
- 字符指针对象和相应的字符数组元素比较
- 如何在 c++ 中克隆为弱指针对象
- 指针对象使用CPP中的指针表达式进行比较
- Visual Studio中智能指针对象的自定义视图
- 使用指针 /对象 /模板参数正确构成
- 如果我确定只有一个线程一次处理指针/对象,则C/C 仍应使用同步
- 如何将值分配给指针对象 c++
- 更改智能指针对象,然后访问它