shared_ptr 链表释放
时间: 2023-09-19 16:09:56 浏览: 51
当使用 shared_ptr 管理链表时,可以避免手动释放内存,因为 shared_ptr 会自动在不再需要时释放所管理的对象。
下面是一个示例代码,展示了如何使用 shared_ptr 管理链表的释放:
```cpp
#include <memory>
struct Node {
int data;
std::shared_ptr<Node> next;
Node(int val) : data(val), next(nullptr) {}
};
int main() {
std::shared_ptr<Node> head = std::make_shared<Node>(1);
std::shared_ptr<Node> node1 = std::make_shared<Node>(2);
std::shared_ptr<Node> node2 = std::make_shared<Node>(3);
head->next = node1;
node1->next = node2;
// 所有的 shared_ptr 在 main 函数运行结束后会自动释放内存
}
```
在上面的示例中,我们创建了一个链表,其中每个节点都由一个 shared_ptr 管理。当 main 函数运行结束时,所有的 shared_ptr 会自动释放它们所管理的节点对象,无需手动释放。
这样,使用 shared_ptr 可以避免内存泄漏和悬挂指针等问题,提高代码的健壮性和可维护性。
相关问题
c++如何用智能指针实现链表的几种方式,包括用unique_ptr实现、shared_ptr实现
链表是一种常见的数据结构,智能指针可以帮助我们管理链表节点的内存,避免内存泄漏和野指针的问题。下面介绍几种用智能指针实现链表的方式。
1. 用unique_ptr实现
unique_ptr是C++11标准引入的智能指针,每个unique_ptr对象都“独占”所指向的内存资源,即同一时间只能有一个unique_ptr对象拥有该内存资源,其他unique_ptr对象无法访问该内存资源。因此,我们可以用unique_ptr实现链表节点,每个节点拥有唯一的unique_ptr对象。
```c++
#include <memory>
template<typename T>
struct Node {
T data;
std::unique_ptr<Node<T>> next;
Node(T data) : data(std::move(data)), next(nullptr) {}
};
template<typename T>
class LinkedList {
public:
LinkedList() : head(nullptr) {}
void insert(T data) {
std::unique_ptr<Node<T>> newNode = std::make_unique<Node<T>>(std::move(data));
if (!head) {
head = std::move(newNode);
} else {
Node<T>* current = head.get();
while (current->next) {
current = current->next.get();
}
current->next = std::move(newNode);
}
}
private:
std::unique_ptr<Node<T>> head;
};
```
2. 用shared_ptr实现
shared_ptr是C++11标准引入的智能指针,多个shared_ptr对象可以“共享”同一个内存资源,即同一时间可以有多个shared_ptr对象拥有该内存资源。因此,我们可以用shared_ptr实现链表节点,每个节点拥有一个shared_ptr对象,多个节点的shared_ptr对象可以共享同一个内存资源。
```c++
#include <memory>
template<typename T>
struct Node {
T data;
std::shared_ptr<Node<T>> next;
Node(T data) : data(std::move(data)), next(nullptr) {}
};
template<typename T>
class LinkedList {
public:
LinkedList() : head(nullptr) {}
void insert(T data) {
std::shared_ptr<Node<T>> newNode = std::make_shared<Node<T>>(std::move(data));
if (!head) {
head = std::move(newNode);
} else {
std::shared_ptr<Node<T>> current = head;
while (current->next) {
current = current->next;
}
current->next = std::move(newNode);
}
}
private:
std::shared_ptr<Node<T>> head;
};
```
以上是用智能指针实现链表的两种方式,其中unique_ptr和shared_ptr都可以有效避免内存泄漏和野指针的问题。选择哪种方式取决于具体需求和场景,需要根据实际情况进行选择。
objectarx二次开发自定义实体,实体数据是一个结构体tagGmyMapTable,结构体tagGmyMapTable中包含另外一个结构体tagGeometryWithID geoID、一个类指针ElmGraphic* geoData、一个bool变量isBatchDrawBroadWise,而结构体tagGeometryWithID保存的是一个unsigned long long int elementID数据和一个vector<wstring> exDataVec,类指针geoData中有一个vector<shared_ptr<CBaseGeometry>> ptrgeos, CBaseGeometry是个基类,使用时会根据CBaseGeometry中的int型type的参数判断强制转换成不同子类进行绘制,子类中的数据也不相同,CBaseGeometry中包含了vector<AcGePoint3D> Pts以及字符串、int等数据,请问如何保存读取这些数据到dwg
在使用ObjectARX二次开发自定义实体时,需要实现AcDbEntity的相关接口函数,以便在dwg文件中保存和读取实体数据。
对于结构体tagGmyMapTable和tagGeometryWithID中的数据,可以使用AcDbXrecord类来进行保存和读取。AcDbXrecord是一种特殊的实体,其内部可以保存任何类型的数据,包括结构体、类指针等。
以下是保存tagGmyMapTable数据到dwg文件的示例代码:
```cpp
void saveMapTableToDwg(const AcDbObjectId& id, const tagGmyMapTable& mapTable)
{
// 打开实体对象
AcDbEntityPtr pEnt(id, AcDb::kForWrite);
if (pEnt.isNull())
return;
// 创建Xrecord对象
AcDbXrecordPtr pXrec = new AcDbXrecord;
pXrec->setXlateReferences(true);
// 将tagGmyMapTable数据保存到Xrecord中
resbuf* pBuf = acutBuildList(
RTSHORT, 0, // 数据版本号
RTSHORT, sizeof(tagGmyMapTable), // 数据长度
RTENDBR);
pBuf->rbnext = acutBuildList(
RTLONG, mapTable.geoID, // 保存tagGeometryWithID数据
RTLONG, mapTable.isBatchDrawBroadWise,
RTLONG, mapTable.geoData->ptrgeos.size(),
RTENDBR);
pBuf->rbnext->rbnext = acutBuildList(
RTLONG, mapTable.geoData->type,
RTLONG, mapTable.geoData->Pts.size(),
RTENDBR);
pBuf->rbnext->rbnext->rbnext = acutBuildList(
RTSTR, mapTable.geoData->name.c_str(),
RTSTR, mapTable.geoData->desc.c_str(),
RTLONG, mapTable.geoData->color,
RTENDBR);
pXrec->setFromRbChain(*pBuf);
acutRelRb(pBuf);
// 将Xrecord对象保存到实体中
pEnt->setXData(pXrec);
}
```
同样,可以使用AcDbXrecord类将tagGeometryWithID和CBaseGeometry的数据保存到dwg文件中。
读取数据时,需要先读取AcDbXrecord对象,然后解析其中的数据,重新构造出原来的结构体、类指针等。
以下是从dwg文件中读取tagGmyMapTable数据的示例代码:
```cpp
tagGmyMapTable getMapTableFromDwg(const AcDbObjectId& id)
{
tagGmyMapTable mapTable;
// 打开实体对象
AcDbEntityPtr pEnt(id, AcDb::kForRead);
if (pEnt.isNull())
return mapTable;
// 读取Xrecord对象
AcDbObjectIdArray xrecIds;
pEnt->getXData(xrecIds);
AcDbXrecordPtr pXrec;
if (xrecIds.length() > 0)
acdbOpenObject(pXrec, xrecIds[0], AcDb::kForRead);
if (pXrec.isNull())
return mapTable;
// 解析Xrecord中的数据
resbuf* pBuf = pXrec->rbChain();
if (pBuf == nullptr)
return mapTable;
// 读取tagGeometryWithID数据
if (pBuf->restype == RTSHORT && pBuf->resval.rint == 0)
{
pBuf = pBuf->rbnext;
if (pBuf == nullptr || pBuf->restype != RTSHORT || pBuf->resval.rint != sizeof(tagGeometryWithID))
return mapTable;
tagGeometryWithID geoID;
pBuf = pBuf->rbnext;
if (pBuf == nullptr || pBuf->restype != RTLONG)
return mapTable;
geoID.elementID = pBuf->resval.rlong;
pBuf = pBuf->rbnext;
if (pBuf == nullptr || pBuf->restype != RTLONG)
return mapTable;
mapTable.isBatchDrawBroadWise = pBuf->resval.rlong;
pBuf = pBuf->rbnext;
if (pBuf == nullptr || pBuf->restype != RTLONG)
return mapTable;
int geoDataSize = pBuf->resval.rlong;
pBuf = pBuf->rbnext;
if (pBuf == nullptr || pBuf->restype != RTLONG)
return mapTable;
geoID.type = pBuf->resval.rlong;
pBuf = pBuf->rbnext;
if (pBuf == nullptr || pBuf->restype != RTLONG)
return mapTable;
int ptsSize = pBuf->resval.rlong;
pBuf = pBuf->rbnext;
if (pBuf == nullptr || pBuf->restype != RTSTR)
return mapTable;
geoID.name = pBuf->resval.rstring;
pBuf = pBuf->rbnext;
if (pBuf == nullptr || pBuf->restype != RTSTR)
return mapTable;
geoID.desc = pBuf->resval.rstring;
pBuf = pBuf->rbnext;
if (pBuf == nullptr || pBuf->restype != RTLONG)
return mapTable;
geoID.color = pBuf->resval.rlong;
// 读取vector<wstring>数据
for (int i = 0; i < exDataSize; i++)
{
pBuf = pBuf->rbnext;
if (pBuf == nullptr || pBuf->restype != RTSTR)
return mapTable;
geoID.exDataVec.push_back(pBuf->resval.rstring);
}
mapTable.geoData = new tagGeometryWithID(geoID);
}
// 读取vector<shared_ptr<CBaseGeometry>>数据
if (pBuf->restype == RTSHORT && pBuf->resval.rint == 1)
{
pBuf = pBuf->rbnext;
if (pBuf == nullptr || pBuf->restype != RTLONG)
return mapTable;
int ptrgeosSize = pBuf->resval.rlong;
pBuf = pBuf->rbnext;
if (pBuf == nullptr || pBuf->restype != RTLONG)
return mapTable;
int type = pBuf->resval.rlong;
vector<AcGePoint3d> pts;
int ptsSize = 0;
string name, desc;
int color = 0;
// 读取vector<AcGePoint3d>数据
for (int i = 0; i < ptsSize; i++)
{
pBuf = pBuf->rbnext;
if (pBuf == nullptr || pBuf->restype != RTPOINT)
return mapTable;
pts.push_back(asPnt3d(pBuf));
}
// 读取字符串、int等数据
while (true)
{
pBuf = pBuf->rbnext;
if (pBuf == nullptr)
break;
if (pBuf->restype == RTSTR)
{
if (name.empty())
name = pBuf->resval.rstring;
else if (desc.empty())
desc = pBuf->resval.rstring;
}
else if (pBuf->restype == RTLONG)
{
if (color == 0)
color = pBuf->resval.rlong;
}
}
// 根据type创建不同的子类对象
shared_ptr<CBaseGeometry> pGeo;
switch (type)
{
case 1:
pGeo = make_shared<CLine>(pts, name, desc, color);
break;
case 2:
pGeo = make_shared<CArc>(pts, name, desc, color);
break;
// 其他子类
}
mapTable.geoData->ptrgeos.push_back(pGeo);
}
return mapTable;
}
```
需要注意的是,保存和读取数据时,需要将数据按照一定的格式打包成resbuf链表,以便于保存到Xrecord中或从Xrecord中读取。对于不同类型的数据,需要使用不同的resbuf类型来保存和读取。具体的格式可以根据实际需要进行调整。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)