解除引用已初始化为list.begin的迭代器时收到段错误

时间:2013-01-24 17:36:32

标签: c++ list stl iterator segmentation-fault

我在解除引用已初始化为list.begin()的迭代器时遇到段错误。

list<data>::iterator it;
for(int i=0; i< n; i++)    
{
    it = (list_empty[i]).begin();
    while(it != (list_empty[i]).end())
    {
        cout<<"PROBLEM HERE: size="<<it->process.size<<endl;
        //cout<<"log file i="<<i<<endl;
        log_file_start(current_time, it,"list of empty");
        it++;
    }
}

这是来自gdb的错误:

Program received signal SIGSEGV, Segmentation fault.
[Switching to process 2888]
0x0804ecc0 in log_file_buddy (list_delay=..., list_vp=..., 
    list_empty=0x805a00c, method=..., current_time=0, n=10)
    at fuctions_of_mm.cpp:425 
425         cout<<"PROBLEM HERE: size="<<it->process.size << endl;

输出bt为full的gdb,表明迭代器为NULL。

(gdb) bt full
#0  0x0804ecc0 in log_file_buddy (list_delay=..., list_vp=..., 
    list_empty=0x805a00c, method=..., current_time=0, n=10)
    at fuctions_of_mm.cpp:425
        i = 8
        it = {_M_node = 0x0}
        out = <incomplete type>

虽然迭代器为NULL,但会计算it != (list_empty[i]).end()。 怎么了?

修改 对不起,我很抱歉。 就是这样:list_empty = new list<data>[n]; N是给定的参数,表示:2 ^ {N} = Size_of_Memory

编辑#2: 这是定义:

typedef struct data{
    int position;
    vp proccess;
    int delay;
    int current_life;
    int time_start;
    int time_stop;
    int part_of_memory; 
    bool operator ==(const data& st)
    {
      return proccess.pid == st.proccess.pid;
    }
}data;

vp的定义:

    typedef struct {
    int size;
    int pid;//prosdiorisths diergasias
 }vp

n的值:n = 10

list_empty的声明是list&lt;数据&gt; * list_empty;

2 个答案:

答案 0 :(得分:0)

这对我有用:

#include <list>
using std::list;

const int n = 10;

typedef struct {
    int size;
    int pid;//prosdiorisths diergasias
}vp;


typedef struct data{
    int position;
    vp process;
    int delay;
    int current_life;
    int time_start;
    int time_stop;
    int part_of_memory;
    bool operator ==(const data& st)
    {
    return process.pid == st.process.pid;
    }
}data;

int main()
{

    list<data>* list_empty = new list<data>[n];
    list<data>::iterator it;
    for(int i=0; i< n; i++)
    {
        it = (list_empty[i]).begin();
        while(it != (list_empty[i]).end())
        {
            cout<<"PROBLEM HERE: size="<<it->process.size<<endl;
            //cout<<"log file i="<<i<<endl;
            //log_file_start(current_time, it,"list of empty");
            it++;
        }
    }
}

答案 1 :(得分:0)

什么是it

it_empty = new list<data>::iterator[n];
for(int i=0; i< n; i++)    
{
    auto it = (list_empty[i]).begin();
    while(it != (list_empty[i]).end())
    {
        cout<<"PROBLEM HERE: size="<<it->process.size<<endl;
        //cout<<"log file i="<<i<<endl;
        log_file_start(current_time, it,"list of empty");
        it++;
    }
}