Segmentation fault when accessing a list inside a struct

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-10 23:32:53

问题


I'll try to make things clear.

Here is my struct:

struct scopeList{
       int id;
       list<int> internal_list;
};
typedef struct scopeList scopeList_T;

Here is the code that gives me the segmentation.

int main(){
    scopeList_T* t1 = (scopeList_T*) malloc(sizeof(scopeList_T));
    t1->id = 5; //works fine
    t1->internal_list.push_front(5); //segmentation fault
} 

Since I am allocating memory and accessing id is fine, why is does this give me a segmentation fault? Do I have to do something special to the list first?

Thanks!


回答1:


Use new instead of malloc!

sopeList_T* t1 = new scopeList_T;

malloc doesn't run any constructors. When you need to release that struct, use delete rather than free - you can't free objects allocated with new (and free doesn't call destructors).

You don't need that typedef either, the struct declaration is enough.

struct scopeList {
    int id;
    list<int> internal_list;
};

int main()
{
    scopeList *t = new scopeList;
    ....
    delete t;
    ....
}



回答2:


You don't initialize the list using a constructor, thus leaving invalid data at the position of internal_list.




回答3:


The right answers have been given above: You have allocated the memory of your struct but haven't run any constructors for the subobjects which are in uninitialized state. I have to insist: This is an absolute no-go. NEVER mix up calls to alloc & Co. with C++ code.




回答4:


Since malloc doesn't call constructor, you've to do either of these two ways:

  1. Use new to allocate the memory as well the construct the object:

    sopeList_T* t1 = new scopeList_T;//it allocates and then call the ctor!
    
    //use delete to deallocate the memory
    delete t1;
    
  2. Or use malloc to allocate the memory which you're already doing, and use placement new to construct the object:

    scopeList_T* t1 = (scopeList_T*) malloc(sizeof(scopeList_T)); //allocation
    t1 = new (t1) scopeList_T; //it calls the ctor 
    
    //use free to deallocate the memory
    t1->~scopeList_T(); //call the destructor explicitly - necessary!
    free(t1);
    



回答5:


Allocating memory is not enough. You must also call the constructor.

The most common and recommended way of simple dynamic allocation in C++ is

scopeList_T* t1 = new scopeList_T;

is allocates memory and then calls constuctor.

After you're done with the stucture you have to delete the object like this

delete t1;

ADD:

If you really need to use other memory allocator (like malloc/free or something of your own design) then you have to allocate memory and call the placement new (it is like calling the constuctor explicitly). When you're done with the object you have to call the destructor explicitly and then deallocate memory. Important thing: memory allocated for the object must meet the alignment requirements for this object type.

Example:

// allocating memory
void* p = my_alloc( sizeof(scopeList_T) );

if( p == NULL )
{
// report allocation error and throw or return
}

// placement new operator
scopeList_T* t1 = new(p) scopeList_T; // t1 == p

// do some thing with the object
// .............................

// call destructor explicitly
t1->~scopeList_T();
// free memory
my_free(p); // or free(t1); that is the same


来源:https://stackoverflow.com/questions/5522123/segmentation-fault-when-accessing-a-list-inside-a-struct

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!