问题
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:
Use
newto 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;Or use
mallocto allocate the memory which you're already doing, and use placementnewto 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