首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

请教上面的代码有啥有关问题

2012-09-09 
请问下面的代码有啥问题?请问下面的代码错在了那里?C/C++ code#include iostream#include vector#incl

请问下面的代码有啥问题?
请问下面的代码错在了那里?

C/C++ code
#include <iostream>#include <vector>#include <list>using namespace std;template <typename container>container removeEveryOtherItem(container &lst){    typename container::iterator i = lst.begin();    while(i != lst.end()){        i = lst.erase(i);        if(i != lst.end())        {            i++;        }    }}int main(){    list<int> lst;    lst.push_back(1);    lst.push_back(2);    lst.push_back(3);    lst.push_back(4);    lst.push_back(5);    list<int>::iterator i = lst.begin();    while(i != lst.end())    {        cout << *i << endl;        i++;    }    removeEveryOtherItem< list<int> > (lst);}


运行输出:
1
2
3
4
5
Segmentation fault (core dumped)

使用valgrind工具调试,输出
==26183== Memcheck, a memory error detector
==26183== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
==26183== Using Valgrind-3.6.0 and LibVEX; rerun with -h for copyright info
==26183== Command: ./erase
==26183== 
1
2
3
4
5
==26183== Conditional jump or move depends on uninitialised value(s)
==26183== at 0x400EB4: std::_List_base<int, std::allocator<int> >::_M_clear() (list.tcc:69)
==26183== by 0x400C38: std::_List_base<int, std::allocator<int> >::~_List_base() (stl_list.h:360)
==26183== by 0x400BE7: std::list<int, std::allocator<int> >::~list() (stl_list.h:418)
==26183== by 0x400B2C: main (erase.cpp:31)
==26183== 
==26183== Use of uninitialised value of size 8
==26183== at 0x400E54: std::_List_base<int, std::allocator<int> >::_M_clear() (list.tcc:72)
==26183== by 0x400C38: std::_List_base<int, std::allocator<int> >::~_List_base() (stl_list.h:360)
==26183== by 0x400BE7: std::list<int, std::allocator<int> >::~list() (stl_list.h:418)
==26183== by 0x400B2C: main (erase.cpp:31)
==26183== 
==26183== Invalid read of size 8
==26183== at 0x400E54: std::_List_base<int, std::allocator<int> >::_M_clear() (list.tcc:72)
==26183== by 0x400C38: std::_List_base<int, std::allocator<int> >::~_List_base() (stl_list.h:360)
==26183== by 0x400BE7: std::list<int, std::allocator<int> >::~list() (stl_list.h:418)
==26183== by 0x400B2C: main (erase.cpp:31)
==26183== Address 0x0 is not stack'd, malloc'd or (recently) free'd
==26183== 
==26183== 
==26183== Process terminating with default action of signal 11 (SIGSEGV)
==26183== Access not within mapped region at address 0x0
==26183== at 0x400E54: std::_List_base<int, std::allocator<int> >::_M_clear() (list.tcc:72)
==26183== by 0x400C38: std::_List_base<int, std::allocator<int> >::~_List_base() (stl_list.h:360)
==26183== by 0x400BE7: std::list<int, std::allocator<int> >::~list() (stl_list.h:418)
==26183== by 0x400B2C: main (erase.cpp:31)
==26183== If you believe this happened as a result of a stack
==26183== overflow in your program's main thread (unlikely but
==26183== possible), you can try to increase the size of the
==26183== main thread stack using the --main-stacksize= flag.
==26183== The main thread stack size used in this run was 10485760.
==26183== 
==26183== HEAP SUMMARY:
==26183== in use at exit: 48 bytes in 2 blocks
==26183== total heap usage: 5 allocs, 3 frees, 120 bytes allocated
==26183== 
==26183== LEAK SUMMARY:
==26183== definitely lost: 0 bytes in 0 blocks
==26183== indirectly lost: 0 bytes in 0 blocks
==26183== possibly lost: 0 bytes in 0 blocks
==26183== still reachable: 48 bytes in 2 blocks


==26183== suppressed: 0 bytes in 0 blocks
==26183== Rerun with --leak-check=full to see details of leaked memory
==26183== 
==26183== For counts of detected and suppressed errors, rerun with: -v
==26183== Use --track-origins=yes to see where uninitialised values come from
==26183== ERROR SUMMARY: 3 errors from 3 contexts (suppressed: 6 from 6)
Segmentation fault (core dumped)

[解决办法]
valgrind提示的still reachable: 48 bytes in 2 blocks 可能指的是STL的内存池
[解决办法]
你贴的代码没有问题,我用VC2008跑了。
你是用什么编译工具编的?
valgrind是用来检查内存泄露的。你的程序异常退出,所以list中有内存没有释放。
[解决办法]
我用
gcc version 4.1.2 20070115 (SUSE Linux)
编了跑也没问题呀。
你贴的代码和你跑的代码是不是不一样呀。

valgrind没用过,在代码中它要注入代码吗。你把注入的代码注释掉试试。

[解决办法]
函数原型为container removeEveryOtherItem(container &lst);
所以必须加上返回值 return lst;
或者将函数改为void removeEveryOtherItem(container &lst);
这样就没问题了

热点排行