关于一个内存泄露的问题

是这样子的
这样子的代码居然能够正常运行

#include <iostream>
#include <vector>
using namespace std;
vector<int>::iterator fun(vector<int>  t){
  return t.begin()+2;//返回一个局部对象的迭代器
}
int main(){
  vector<int> test = {1,2,3,4};
  auto p1 = fun(test);
  cout << *p1 << endl;//对之前局部的迭代器进行解引用,应该是不行的
}
// 但是能够正常的输出3

不清楚为什么
使用内存检测工具检查了一下

valgrind --tool=memcheck --leak-check=full --show-reachable=yes ./test.out
==4677== Memcheck, a memory error detector
==4677== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==4677== Using Valgrind-3.12.0 and LibVEX; rerun with -h for copyright info
==4677== Command: ./test.out
==4677== 
==4677== Invalid read of size 4
==4677==    at 0x400BA8: main (t.cpp:13)
==4677==  Address 0x5ab4cd8 is 8 bytes inside a block of size 16 free'd
==4677==    at 0x4C2F160: operator delete(void*) (vg_replace_malloc.c:576)
==4677==    by 0x4016AB: __gnu_cxx::new_allocator<int>::deallocate(int*, unsigned long) (new_allocator.h:110)
==4677==    by 0x4015B8: std::allocator_traits<std::allocator<int> >::deallocate(std::allocator<int>&, int*, unsigned long) (alloc_traits.h:517)
==4677==    by 0x4013A5: std::_Vector_base<int, std::allocator<int> >::_M_deallocate(int*, unsigned long) (stl_vector.h:178)
==4677==    by 0x401094: std::_Vector_base<int, std::allocator<int> >::~_Vector_base() (stl_vector.h:160)
==4677==    by 0x400EB2: std::vector<int, std::allocator<int> >::~vector() (stl_vector.h:425)
==4677==    by 0x400B9B: main (t.cpp:12)
==4677==  Block was alloc'd at
==4677==    at 0x4C2E1D6: operator new(unsigned long) (vg_replace_malloc.c:334)
==4677==    by 0x4016EF: __gnu_cxx::new_allocator<int>::allocate(unsigned long, void const*) (new_allocator.h:104)
==4677==    by 0x40160D: std::allocator_traits<std::allocator<int> >::allocate(std::allocator<int>&, unsigned long) (alloc_traits.h:491)
==4677==    by 0x401435: std::_Vector_base<int, std::allocator<int> >::_M_allocate(unsigned long) (stl_vector.h:170)
==4677==    by 0x401508: std::_Vector_base<int, std::allocator<int> >::_M_create_storage(unsigned long) (stl_vector.h:185)
==4677==    by 0x40122C: std::_Vector_base<int, std::allocator<int> >::_Vector_base(unsigned long, std::allocator<int> const&) (stl_vector.h:136)
==4677==    by 0x400F19: std::vector<int, std::allocator<int> >::vector(std::vector<int, std::allocator<int> > const&) (stl_vector.h:320)
==4677==    by 0x400B7F: main (t.cpp:12)
==4677== 
3
==4677== 
==4677== HEAP SUMMARY:
==4677==     in use at exit: 72,704 bytes in 1 blocks
==4677==   total heap usage: 4 allocs, 3 frees, 73,760 bytes allocated
==4677== 
==4677== 72,704 bytes in 1 blocks are still reachable in loss record 1 of 1
==4677==    at 0x4C2DBB6: malloc (vg_replace_malloc.c:299)
==4677==    by 0x4EC2EFF: ??? (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21)
==4677==    by 0x40104E9: call_init.part.0 (dl-init.c:72)
==4677==    by 0x40105FA: call_init (dl-init.c:30)
==4677==    by 0x40105FA: _dl_init (dl-init.c:120)
==4677==    by 0x4000CF9: ??? (in /lib/x86_64-linux-gnu/ld-2.23.so)
==4677== 
==4677== LEAK SUMMARY:
==4677==    definitely lost: 0 bytes in 0 blocks
==4677==    indirectly lost: 0 bytes in 0 blocks
==4677==      possibly lost: 0 bytes in 0 blocks
==4677==    still reachable: 72,704 bytes in 1 blocks
==4677==         suppressed: 0 bytes in 0 blocks
==4677== 
==4677== For counts of detected and suppressed errors, rerun with: -v
==4677== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

后来拿了另外一个代码进行测试

#include <iostream>
#include <vector>
using namespace std;
vector<int>::iterator fun(vector<int>&  t){//修改了,变成传引用
  return t.begin()+2;
}
int main(){
  vector<int> test = {1,2,3,4};
  auto p1 = fun(test);
  cout << *p1 << endl;
}

这个是肯定没问题的
同样的内存检测工具进行

valgrind --tool=memcheck --leak-check=full --show-reachable=yes ./test.out
==4743== Memcheck, a memory error detector
==4743== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==4743== Using Valgrind-3.12.0 and LibVEX; rerun with -h for copyright info
==4743== Command: ./test.out
==4743== 
3
==4743== 
==4743== HEAP SUMMARY:
==4743==     in use at exit: 72,704 bytes in 1 blocks
==4743==   total heap usage: 3 allocs, 2 frees, 73,744 bytes allocated
==4743== 
==4743== 72,704 bytes in 1 blocks are still reachable in loss record 1 of 1
==4743==    at 0x4C2DBB6: malloc (vg_replace_malloc.c:299)
==4743==    by 0x4EC2EFF: ??? (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21)
==4743==    by 0x40104E9: call_init.part.0 (dl-init.c:72)
==4743==    by 0x40105FA: call_init (dl-init.c:30)
==4743==    by 0x40105FA: _dl_init (dl-init.c:120)
==4743==    by 0x4000CF9: ??? (in /lib/x86_64-linux-gnu/ld-2.23.so)
==4743== 
==4743== LEAK SUMMARY:
==4743==    definitely lost: 0 bytes in 0 blocks
==4743==    indirectly lost: 0 bytes in 0 blocks
==4743==      possibly lost: 0 bytes in 0 blocks
==4743==    still reachable: 72,704 bytes in 1 blocks
==4743==         suppressed: 0 bytes in 0 blocks
==4743== 
==4743== For counts of detected and suppressed errors, rerun with: -v
==4743== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

同样能够正常运行
不过内存检测工具还是检查出问题来了

问题:
不知道为什么之前那个代码能够正常运作

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
提供的源码资源涵盖了Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!
提供的源码资源涵盖了小程序应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!
提供的源码资源涵盖了Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值