[英]resize vector element of a struct - segv
I am trying to resize a vector element of a structure and it causes segv. 我正在尝试调整结构的向量元素的大小,这会导致segv。 But when I did it individually for some small struct it worked fine.
但是,当我针对某些小型结构单独进行操作时,它的效果很好。 I am curious to know how it allocates memory to structure in which there is a vector element that could be resized.
我很好奇,知道它如何将内存分配给可以调整大小的矢量元素的结构。 The below comment line causes segv in first iteration (type_index = 0).
下面的注释行在第一次迭代中导致segv(type_index = 0)。
Structure:- 结构体:-
struct thread_data {
dbPointer_t pObj;
dbObjId_t objId;
dbObjTypeId_t type;
dbObjId_t topCellId;
dbIteratorId_t objsIterId;
int precision;
int64_t shape_objs;
vector<vector<vector<PL_trp_header_t *> > > ps_hdrs;
int pool;
int num_layers;
int to_cell_id;
};
Below is the snippet of code:- 以下是代码段:-
thread_data *t_data[types_length];
for(int type_index=0; type_index < types_length; ++type_index) {
t_data[type_index] = (thread_data*)malloc(sizeof(thread_data));
t_data[type_index]->pObj = NULL;
t_data[type_index]->objId = objId;
t_data[type_index]->type = shape_types[type_index];
t_data[type_index]->topCellId = topCellId;
t_data[type_index]->objsIterId = objsIterId;
t_data[type_index]->precision = nparams.unit_precision;
t_data[type_index]->shape_objs = 0;
t_data[type_index]->ps_hdrs.resize(num_layers); //this line causes segv
t_data[type_index]->pool = pool;
t_data[type_index]->num_layers = num_layers;
t_data[type_index]->to_cell_id = tocell_id;
for (int num = 0; num < num_layers; num++) {
t_data[type_index]->ps_hdrs[num].resize(index_limit);
for (int rows = 0; rows < index_limit; rows++)
t_data[type_index]->ps_hdrs[num][rows].resize(index_limit);
}
for(int i = 0; i < num_layers; i++) {
for (int rows = 0; rows < index_limit; rows++) {
for (int cols = 0; cols < index_limit; cols++) {
t_data[type_index]->ps_hdrs[i][rows][cols] = alloc_hdr(pool);
}
}
}
printf("In main: creating thread %d \n", type_index);
rc_thread = pthread_create(&threads[type_index], NULL, thread_fn, (void *) &t_data[type_index]);
if (rc_thread){
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
free(t_data[type_index]);
}
I think you are allocating your data with malloc
. 我认为您正在使用
malloc
分配数据。 In this case no constructors for your objects and theier members are called. 在这种情况下,不会调用对象和其成员的构造函数。 This works with PODs but not with classes like
vector
. 这适用于POD,但不适用于
vector
类。 In the line with the error you try to access some unitialised memory like an vector
. 在出现错误的那一行中,您尝试访问一些
vector
统一内存。 Try new
and delete
instead of mallac
and free
to solve this isue. 尝试使用
new
和delete
代替mallac
并free
解决此问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.