简体   繁体   English

阵列内存分配不起作用

[英]Array memory Allocation doesn't work

I have the next classes: 我有下课:

class A {
};

class B : public A {
  int num;
};

in my main I have: 在我的主要我有:

int main() {
    A* vec; // A is a class with pure virtual functions
    vec = new B[2]; // want to create a vector of B
}

vec[0] is defined correctly, but vec[1] is NULL. vec [0]的定义正确,但vec [1]为NULL。 why didn't it allocate me a fit memory? 为什么不给我分配合适的记忆?

I don't want to change the lines of the main. 我不想更改主线。 just make it working. 使其工作。

(I know I can change the main into: B* vec = new B[2] but I don't want) (我知道我可以将main更改为:B * vec = new B [2],但我不想)

any help appreciated! 任何帮助表示赞赏!

You cannot treat arrays polymorphically, the C++ language does not support it. 您不能对数组进行多态处理,C ++语言不支持它。 The expression vec[x] uses pointer arithmetic to determine the location of the element. 表达式vec[x]使用指针算术确定元素的位置。 If you are accessing it through a base class pointer it will not work if the size of the objects vary in any way. 如果您通过基类指针访问它,则无论对象大小如何变化,它都将无法工作。

For example, you have base class that is 4 bytes in size and the subclass is 8 bytes in size. 例如,您的基类的大小为4个字节,子类的大小为8个字节。

base *a = new child[4];

When you access a[1] the compiler calculates the offset using the size of the base class. 当您访问a[1] ,编译器将使用基类的大小来计算偏移量。 In this case the offset is 4 bytes which ends up pointing to the middle of the first element. 在这种情况下,偏移量为4个字节,最终指向第一个元素的中间。

I recommend using a std::vector or std::array of pointers with an appropriate smart pointer. 我建议使用带有适当智能指针的std::vectorstd::array指针。

// For arrays that needs to be resized (requires delete for each new)
std::vector<A*> vec(5, NULL);
for(int i = 0; i < vec.size(); i++)
{
    vec[i] = new B();
}

// for arrays that are fixed in size (requires delete for each new)
std::array<A*, 5> vec;
for(int i = 0; i < vec.size(); i++)
{
    vec[i] = new B();
}

// for arrays that are fixed in size with smart pointers
// no delete needed 
std::array<std::unique_ptr<A>, 5> vec;
for(int i = 0; i < vec.size(); i++)
{
    vec[i].reset(new B());
}

如果您希望它是多态的,只需创建一个指针数组

new A*[array_size]

This code snippet illustrates the problem you are having. 此代码段说明了您遇到的问题。

#include <iostream>
using namespace std;
class A {
};

class B : public A {
  int num;
};

int main() {
    A* vec; // A is a class with pure virtual functions
    vec = new B[2]; // want to create a vector of B

    cout << sizeof(vec) << endl;
    cout << sizeof(*vec) << endl;
    cout << sizeof(vec[2]) << endl;
    cout << sizeof(new B()) << endl;
}

In pointer arrithmetic, the size of the type of the pointer you allocated is what is used for incrementing, not the size of the true type of the object it is pointing to. 在指针算术中,分配给您的指针类型的大小是用于增量的大小,而不是它所指向的对象的真实类型的大小。 More simply, the language does not support polymorphic arrays. 更简单地说,该语言不支持多态数组。 This is simply an explanation of why. 这仅仅是原因的解释。

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

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