简体   繁体   English

向量insert()错误:向量迭代器偏移超出范围

[英]vector insert() error: vector iterator offset out of range

For some reason this isn't working for me. 由于某种原因,这对我不起作用。 It gives me the vector iterator out of range error. 它给了我向量迭代器超出范围的错误。

directory_entry TDE("Path");
vector <directory_entry> Temp;
Temp.push_back(TDE);

User_Data->DPath.insert(User_Data->DPath.begin(), Temp.begin(), Temp.end());

But, this works, 但是,这有效

vector <directory_entry> DPath;
directory_entry TDE("Path");
vector <directory_entry> Temp;
Temp.push_back(TDE);

DPath.insert(DPath.begin(), Temp.begin(), Temp.end());

I don't think there is anything wrong with User_Data->DPath because I can push/pop and access elements in it. 我认为User_Data-> DPath没什么问题,因为我可以推送/弹出并访问其中的元素。 But for some reason I can't seam to be able to use insert on it without getting out of range errors. 但是由于某种原因,我无法在不超出范围错误的情况下在其上使用插入。

Does anyone know why this might be? 有谁知道为什么会这样吗?

edit: A popup emerges, debug assertion failed. 编辑:出现一个弹出窗口,调试断言失败。 It gives me a line in the vector header file, 1111, and a message "Expression: vector iterator out of range". 它在向量头文件1111中给我一行,并显示一条消息“表达式:向量迭代器超出范围”。 If I make sure that there is at least one element in User_Data->DPath, and then start at .begin+1, I get "Expression: vector iterator+offset out of range" and it gives me line 157 of the vector header file. 如果我确定User_Data-> DPath中至少有一个元素,然后从.begin + 1开始,则会出现“表达式:向量迭代器+偏移量超出范围”的情况,它为我提供了向量头文件的第157行。

edit: You are all probably right. 编辑:你可能都是正确的。 The g_new0 function does the memory allocation http://developer.gnome.org/glib/2.32/glib-Memory-Allocation.html#g-new0 g_new0函数执行内存分配http://developer.gnome.org/glib/2.32/glib-Memory-Allocation.html#g-new0

struct_type : the type of the elements to allocate. struct_type:要分配的元素的类型。 n_structs : the number of elements to allocate. n_structs:要分配的元素数。 Returns : a pointer to the allocated memory, cast to a pointer to struct_type. 返回:指向已分配内存的指针,转换为struct_type的指针。

typedef struct {
    vector <directory_entry> DPath;
}State;

static gboolian select_dir (ClutterActor *actor, ClutterEvent *event, g_pointer data){
    State *User_Data = (State*)data;

    directory_entry Temp(Path);
    User_Data->DPath.push_back(Temp);

    ...
    return TRUE;
}


int main( argc, char*argv[]){
State *data = g_new0 (State, 1); 

...  

 g_signal_connect(Cluter_Actor, "button-event", G_CALLBACK(select_dir), data)

 ...

 clutter_main();
 g_free(data);
 return 0;
 }

g_new0 is not a drop-in replacement for new g_new0不能替代new

new does two things: allocates memory for an object, and calls the object's constructor. new做两件事:为对象分配内存,并调用对象的构造函数。 g_new0 only does the first, allocating memory. g_new0仅执行第一个分配内存的操作。 You need to call the object's constructor explicitly if you want to use g_new0 . 如果要使用g_new0则需要显式调用该对象的构造函数。 This is done using "placement new": 这可以通过“ placement new”来完成:

State *data = g_new0 (State, 1); 
new (data) State;  // placement new - calls the constructor

The reason calling State 's constructor is important is that it in turn calls the constructor of the vector<directory_entry> member of State, and this is what initializes the vector. 调用State的构造函数很重要的原因是,它依次调用State的vector<directory_entry>成员的构造函数,这就是初始化矢量的原因。 Without initializing the vector properly, you cannot use it. 如果不正确初始化向量,则无法使用它。

Note that since you are calling the constructor explicitly, you will also need to call the destructor explicitly before freeing the memory: 请注意,由于要显式调用构造函数,因此在释放内存之前,还需要显式调用析构函数:

data->~State();  // call destructor
g_free(data);    // free the memory

Is there a reason you are using g_new0 instead of just new ? 您使用g_new0而不是new是有原因的吗?

State *data = new State;
...   // use data
delete data;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM