我需要生成一个列表并返回一个指针,以便其他函数可以使用该列表。该代码工作,但有内存泄漏,因为我使用“新”分配列表的每个新节点。

使用列表后,我必须释放内存。

我的代码如下:

#include

#include

#include

using namespace std;

class linkListClass

{

private:

int data;

auto_ptr nextData;

public:

auto_ptr buildLinkList(const int size);

int getData(){return data;};

int printListBackward(auto_ptr listroot);

};

inline auto_ptr linkListClass::buildLinkList(const int size)

{

linkListClass *trueRoot;

linkListClass *listRoot = new linkListClass ;

linkListClass *trueRoot;

linkListClass *listRoot = new linkListClass ;

// data is random int

for (int i = 0; i < size ; ++i)

{

if (i < size -1)

{

if (i == 0)

{

listRoot->data = rand()%10 ;

listRoot->nextData = auto_ptr(0) ;

trueRoot = listRoot ; // transfer ownership

}

else{

listRoot->nextData = auto_ptr (new linkListClass); // segmentation fault

listRoot->data = rand()%10 ;

listRoot = listRoot->nextData ;

}

}

else

listRoot->nextData = auto_ptr(0) ;

}

cout << "the built list has " << size << " data \n\n" ;

return trueRoot;

}

inline int linkListClass::printListBackward(auto_ptr listroot)

{

int counter =0 ;

stack outputStack;

cout << "print the list forward \n\n" ;

//if (listroot != NULL)

cout << "print the list forward \n\n" ;

//if (listroot != NULL)

if (listroot.get() != 0)

{

do

{

try{

cout << listroot->getData() << " \t " ;

outputStack.push(listroot->data);

listroot = listroot->nextData;

++counter;

cout << "in printListBackward counter is " << counter << endl;

//if (listroot == 0) break;

}

catch(exception& e)

{

cout << "an error is " << e.what() << endl;

return 1;

}

//}while(listroot != 0);

}while(listroot.get() != 0);

cout << "in printListBackward outof do while \n\n " << endl ;

}

else

{

cout << "the input list is null \n\n" << endl;

return 1;

}

cout << endl ;

cout << "there are" << counter << " data in the list \n\n " << endl ;

cout << "print the list backward \n\n" ;

if (outputStack.empty() == 1)

{

cout << "the ouytput queu is empty \n\n " << endl ;

cout << "the ouytput queu is empty \n\n " << endl ;

return 1;

}

else

{

do

{

cout << outputStack.top() << " \t" ;

outputStack.pop();

}while(outputStack.empty() == 0);

}

cout << endl;

cout << "there are" << counter << " data in the list \n\n " << endl ;

return 0 ;

}

int main()

{

const int listSize = 5;

linkListClass linkListObj;

auto_ptr myRoot ; //= linkListClass::buildLinkList(listSize);

myRoot = linkListObj.buildLinkList(listSize);

linkListObj.printListBackward(myRoot);

return 0;

}

// EOF

的代码有段错误,因为auto_ptr的传递指针对象的所有权,使 后

listRoot = listRoot->nextData

链表被打破,listRoot-> nextData一片空白。

我已经试过TR1 :: shared_ptr的和waek_ptr

tr1::weak_ptr wp1 = listRoot->nextData;

listRoot = wp1.lock() ;

listRoot = listRoot->nextData ;

但我得到了编译错误:

listPtSharedptr.cpp:63: error: conversion from linkListClass* to non-scalar type std::tr1::weak_ptr requested listPtSharedptr.cpp:65: error: no match for operator= in listRoot = listRoot.std::tr1::shared_ptr<_tp>::operator-> with _Tp = linkListClass->linkListClass::nextData /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/tr1/boost_shared_ptr.h:486: note: candidates are: std::tr1::shared_ptr& std::tr1::shared_ptr::operator=(const std::tr1::shared_ptr&)

任何帮助将不胜感激。

的感谢!

+0

我不认为你想传递一个auto_ptr <>到printLastBackward(),而只是传递一个常规指针。 –

linux怎么编译sharedptr,如何使用智能指针(例如auto_ptr r shared_ptr)在Linux上使用C++生成链接列表数据结构?...相关推荐

  1. 【C++】智能指针(auto_ptr,shared_ptr,unique_ptr)及 shared_ptr 强引用原理

    C++智能指针(Smart Pointer) 传统指针存在的问题 auto_ptr 智能指针的简单自实现 shared_ptr shared_ptr 内存销毁的原理(强引用) shared_ptr 的 ...

  2. C++11智能指针(unique_ptr、shared_ptr、weak_ptr)boost::scoped_ptr

    C++11智能指针(unique_ptr.shared_ptr.weak_ptr)_-码农小非-的专栏-CSDN博客_c++ shared_ptr weak_ptr 原创)智能指针拾遗 (原创)智能指 ...

  3. 智能指针(unique_ptr、shared_ptr、weak_ptr)

    主要参考链接:C++ 智能指针最佳实践&源码分析 参考链接:C++11 make_shared - 简书 智能指针在 C++11 标准中被引入真正的标准库(C++98 中引入的 auto_pt ...

  4. C++ 智能指针 :内存泄漏、 RAII、智能指针、auto_ptr、unique_ptr、shared_ptr、weak_ptr、定制删除器deleter

    文章目录 内存泄漏 什么是内存泄漏 内存泄漏的危害: 如何避免内存泄漏 RAII 智能指针 auto_ptr unique_ptr shared_ptr 循环引用问题 weak_ptr 定制删除器 内 ...

  5. 智能指针之auto_ptr

    最近在看STL,看到了auto_ptr,发现这个真的是个好东西,不敢自私,所以写篇博文和大家共享.C++标准程序库提供的auto_ptr是一种智能型的指针(smart pointer).可以帮助程序员 ...

  6. C++智能指针:TR1的 shared_ptr 和 weak_ptr 使用介绍

    (所有示例的运行,将#序号所在main()函数去掉序号即可,参考[url]http://www.codeguru.com/cpp/cpp/cpp_mfc/stl/article.php/c15361/ ...

  7. boost库:智能指针(auto_ptr、scoped_ptr、shared_ptr)浅谈

    无论C还是C++中,我们从一开始接触他们,老时就开始强调在使用后要对内存进行释放,以免发生内存泄露,但是,我们总有粗心的时候,C++中提出了只能指针的概念,用只能指针类来管理所申请的内存,伴随着函数的 ...

  8. linux w3m编译过的,yum安装w3m问题 - 优秀的Free OS(Linux)版 - 北大未名BBS

    用各种语句搜索w3m浏览器,还是没有搜到任何资源,而search到的,却不是w3m浏览器: $ yum list w3m 已加载插件:fastestmirror, langpacks, priorit ...

  9. 使用智能指针错误导致内存泄漏_C++智能指针使用的那些事

    指针指针的由来 在C/C++里面,内存管理由开发者自己管理.指针变量总是指向一片内存空间,这片内存空间可以是局部变量.也可以是通过malloc.new申请的.如果申请的内存没有释放,就会导致内存泄漏. ...

最新文章

  1. 我还不懂什么是分布式事务
  2. 搞懂分布式技术19:使用RocketMQ事务消息解决分布式事务
  3. Python 函数参数传递
  4. ENVI中正射校正说明
  5. html 属性中嵌套php,如何在PHP中使用嵌套数组创建HTML数据属性字符串?
  6. android按钮变输入框动画,AnimShopButton 仿饿了么加入购物车旋转控件 - 自带闪转腾挪动画 的按钮。自带海量可定制 UI 属性。在 Re @codeKK Android开源站...
  7. Epic高管:虚幻4引擎目标是不同规模开发商
  8. 一文了解linux内核,一文了解Linux的系统结构
  9. 菜鸟javascript整理2
  10. python中出现nameerror_python类中出现NameError的问题
  11. logisim的安装
  12. win10计算机修改底色,win10系统怎么更换电脑背景?
  13. wgs84坐标系和国内地图坐标系的转换
  14. C语言pow函数的精度问题
  15. 喜大普奔!rgee能用了!R语言也可以使用Google Earth Engine了!
  16. 【无标题】发的东方人
  17. 小米 MIUI系统 安装包 刷机
  18. 易语言调用大漠插件对雷电模拟器进行后台绑定源码
  19. Python技巧——解析式
  20. [PTA]实验3-4 统计字符

热门文章

  1. Linux Jump Label/static-key机制详解
  2. tcmalloc内存分配器分析笔记:基于gperftools-2.4
  3. 内核引导参数IOMMU与INTEL_IOMMU有何不同?
  4. Java实现查看SEGY(.su格式)数据道头字信息的GUI图形用户界面
  5. 服务器排序规修改办法,WordPress手动修改文章排列顺序摆脱按发布时间升降序排列...
  6. angular移除事件绑定事件绑定_002-js的三种事件绑定方式
  7. python 系统时间24小时制_Python 日期和时间
  8. python 预测 位置_Python:核岭回归预测,KRR
  9. nodejs图片读取
  10. python 状态机第三方库_Python 状态机 - osc_8g11urw7的个人空间 - OSCHINA - 中文开源技术交流社区...