【问题标题】:resize vector element of a struct - segv调整结构的向量元素 - segv
【发布时间】:2013-11-16 02:41:02
【问题描述】:

我正在尝试调整结构的向量元素的大小,它会导致 segv。但是当我为一些小结构单独做它时,它工作得很好。我很想知道它如何将内存分配给可以调整大小的向量元素的结构。下面的注释行在第一次迭代中导致 segv (type_index = 0)。

结构:-

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;
};

下面是sn-p的代码:-

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]);
            }

【问题讨论】:

  • 裸指针的三重向量。在那之后有人需要淋浴。

标签: c++ multithreading vector struct dynamic-memory-allocation


【解决方案1】:

我认为您正在使用malloc 分配数据。在这种情况下,不会调用对象及其成员的构造函数。这适用于 POD,但不适用于 vector 之类的类。根据错误,您尝试访问一些未初始化的内存,例如vector。尝试使用newdelete 而不是mallacfree 来解决此问题。

【讨论】:

  • 感谢 jan 的回复,我会试试的。你能解释一下内存分配吗?
  • @crazy_prog 关于内存分配你唯一需要知道的是使用newdelete 或者更好的一些创建智能指针的工厂函数(比如make_shared)。将 RAII (stackoverflow.com/questions/tagged/c%2b%2b%20raii) 视为对象生命周期管理的概念。
猜你喜欢
  • 2012-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-07
  • 2021-08-21
  • 1970-01-01
  • 2018-10-08
  • 1970-01-01
相关资源
最近更新 更多