简体   繁体   English

一个具有向量数据成员的类

[英]One class with a vector data member

I would like to define a class with a vector data member.我想定义一个带有向量数据成员的类。 The class looks as follows该类如下所示

class A{
...
private:
     std::vector<int> v1;
...
};

If I use operator new to allocate memory for class A, the program is OK.如果我使用operator new为A类分配内存,程序就OK了。 However, if I get the memory from the pre-allocated memory and cast the pointer to type A*, the program will crash.但是,如果我从预先分配的内存中获取内存并将指针强制转换为类型 A*,则程序将崩溃。

A* a = new A;
A* b = (A*)pre_allocated_memory_pointer.

I need one vector with variable size and hope to get the memory for A from one pre-allocated memory.我需要一个大小可变的向量,并希望从一个预先分配的内存中获取 A 的内存。 Do you have any idea about the problem?你对这个问题有什么想法吗?

An std::vector is an object that requires initialization, you cannot just allocate memory and pretend you've got a vector . std::vector是一个需要初始化的对象,你不能只分配内存并假装你有一个vector

If you need to control where to get the memory from the solution is defining operator::new for your class.如果您需要控制从解决方案中获取内存的位置,请为您的类定义operator::new

struct MyClass {
    std::vector<int> x;
    ... other stuff ...

    void *operator new(size_t sz) {
        ... get somewhere sz bytes and return a pointer to them ...
    }

    void operator delete(void *p) {
        ... the memory is now free ...
    }
};

Another option is instead to specify where to allocate the object using placement new:另一种选择是使用placement new 指定分配对象的位置:

struct MyClass {
    std::vector<int> x;
    ... other stuff ...
};

void foo() {
    void * p = ... get enough memory for sizeof(MyClass) ...

    MyClass *mcp = new (p) MyClass();

    ... later ...

    mcp->~MyClass(); // Call destructor

    ... the memory now can be reused ...
}

Note however that std::vector manages itself the memory for the contained elements and therefore you'll need to use stl "allocators" if you want to control where the memory it needs is coming from.但是请注意, std::vector自行管理所包含元素的内存,因此如果您想控制它所需的内存来自何处,则需要使用 stl “分配器”。

It is not enough to cast the pre-allocated memory poiner to your user-defined type unless this UDT is "trivial".除非此 UDT 是“微不足道的”,否则将预分配的内存指针强制转换为用户定义的类型是不够的。

Instead, you may want to use the placement new expression to actually call the constructor of your type at the provided region of memory:相反,您可能希望使用放置 new表达式在提供的内存区域实际调用您的类型的构造函数:

A* b = new(pre_allocated_memory_pointer) A();

Of course, you need to ensure that your memory is properly aligned and can fit the whole object (ie its size is >= sizeof(A) ) beforehand.当然,您需要事先确保您的内存正确对齐并且可以适合整个对象(即其大小 >= sizeof(A) )。

Don't also forget to explicitly call the destructor for this object before de-allocating the underlying memory.在取消分配底层内存之前,不要忘记显式调用此对象的析构函数。

b.~A();
deallocate(pre_allocated_memory_pointer);

As I understand your question you are confusing the data memory of the std::vector with the memory it takes up as a member.据我了解您的问题,您将std::vector的数据内存与其作为成员占用的内存混淆了。

If you convert pre_allocated_memory_pointer to A* , then no constructor got called and you have an invalid object there.如果您将pre_allocated_memory_pointer转换为A* ,则不会调用构造函数并且您在那里有一个无效的对象。 This means that the v1 member will not have been constructed and hence no memory has been allocated for the vector.这意味着不会构造v1成员,因此没有为向量分配内存。

You could use placement new to construct the A instance at the pre_allocated_memory_pointer position but I doubt that is what you want.您可以使用placement new 在pre_allocated_memory_pointer位置构造A实例,但我怀疑这是否是您想要的。

In my opinion you want a custom allocator for the vector that gets the memory for the vector's data from the preallocated memory pool.在我看来,您需要一个自定义的向量分配器,从预分配的内存池中获取向量数据的内存。

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

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