简体   繁体   English

无法正确推回结构C ++中的向量

[英]Cannot properly push back on vector that is in a struct C++

I currently have a struct p that holds a vector of another struct called pg. 我目前有一个结构p,其中包含另一个称为pg的结构的向量。 Here is the process struct: 这是过程结构:

struct p{
int i;
int s;
vector<pg*> pT;

void addP(struct pg* nP){
    pT.push_back(nP);
}

}; };

Somewhere else in the program, I create a new p struct by doing: 在程序的其他地方,我通过执行以下操作创建了一个新的p结构:

struct p *p1= (struct p*) malloc(sizeof(struct p));

and then assign values to id and size. 然后为id和size分配值。 I want to add a new page to the struct's pT vector. 我想向结构的pT向量添加一个新页面。 So I decide I can do p1-> addP(makeP()), where makePreturns a pointer to a pg struct, pg*. 因此,我决定可以执行p1-> addP(makeP()),其中makePreturns指向pg结构pg *的指针。

Here is a link to what gdb shows up when it segfaults. 这是gdb出现段故障时显示的链接。 It also mentions that it is happening on the push_back line. 它还提到它在push_back线上发生。 http://imgur.com/KZOGnI9 http://imgur.com/KZOGnI9

Is it something wrong with the pointers? 指针有问题吗? Or am I not properly allocating memory for the vector? 还是我没有为该向量正确分配内存?

Using malloc in C++ is almost always a bad idea. 在C ++中使用malloc几乎总是一个坏主意。 In your case, this leads to the constructor for struct process being not called, which in turn means the constructor of its member pageTable is not called, too. 在您的情况下,这导致未调用struct process的构造struct process ,这又意味着也未调用其成员pageTable的构造函数。

Therefore, you're calling push_back on an uninitialized vector, resulting in undefined behavior (and in your case, a crash). 因此,您在未初始化的向量上调用push_back ,导致未定义的行为(在您的情况下,这是崩溃)。

malloc presumably only works with "plain old datatypes", ie types that do not need any construction (have only a default constructor and only plain old datatypes as members). malloc可能仅适用于“普通旧数据类型”,即不需要任何构造的类型(仅具有默认构造函数,而仅普通普通数据类型作为成员)。

The correct way to allocate objects on the heap in C++ is using new , which looks like this: 在C ++中,在堆上分配对象的正确方法是使用new ,如下所示:

process *p = new process; // possibly add constructor arguments here

An even better way would be to use smart pointers, like std::unique_ptr or, if shared ownership is required, std::shared_ptr . 更好的方法是使用智能指针,例如std::unique_ptr或者,如果需要共享所有权,则使用std::shared_ptr

An even better approach is to not use pointers at all, especially for the objects in your vector, but probably also for your struct process . 一个更好的做法是在所有不使用指针,尤其是您的载体的对象,但也可能为你的struct process Use them only if you have to! 仅在必要时使用它们!

It is not correct to malloc your process object, since malloc won't call the constructor of the struct, which calls the constructor of std::vector<page*> . malloc分配process对象是不正确的,因为malloc不会调用struct的构造函数,而struct会调用std::vector<page*>的构造函数。

If possible, make your process an ordinary automatic object: 如果可能,使您的process成为普通的自动对象:

process proc;

If you really need dynamic allocation, use new instead of malloc : 如果您确实需要动态分配,请使用new而不是malloc

process* proc = new process;

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

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