在堆上创建对象是否需要对象的大小?

发布于 2024-07-23 08:04:13 字数 259 浏览 9 评论 0原文

当编译器需要知道 C(类)的大小时 对象:例如,当分配一个 C 放在堆栈上或作为直接持有 另一种类型的成员

来自C++ 编码标准:101 条规则、指南和最佳实践

这是否意味着对于堆分配的对象,大小不是必需的?

Class C;//just forward declaration
C * objc = new C();

When compiler need to know the size of a C (class)
object: For example, when allocating a
C on the stack or as a directly-held
member of another type

From C++ Coding Standards: 101 Rules, Guidelines, and Best Practices

Does that mean for a heap allocated object, size is not necessary?

Class C;//just forward declaration
C * objc = new C();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

恰似旧人归 2024-07-30 08:04:13

不,该列表仅作为示例,并不排除任何情况。 显然,在堆分配中必须知道对象大小,以便可以分配正确数量的内存。

No, this list is by way of example and not of exclusion. Obviously the object size must be known in heap allocation so the right amount of memory can be allocated.

并安 2024-07-30 08:04:13

回答您的具体问题:

这是否意味着堆分配
对象大小不是必需的?

Class C;//只是前向声明 
  C * objc = new C(); 
  

C++ 不会让你这么做。

即使它可以让您通过稍后神奇地解析大小来对不完整的类型执行“new”(我可以想象,通过链接器的合作,这在技术上是可能的),该尝试也会失败在编译时,至少有两个原因:

  1. operator new 只能与完整类型一起使用。 根据 C++98 标准 5.3.4 - “[分配的]类型应是完整的对象类型,但不是抽象类类型或其数组”

  2. 编译器不知道存在哪些构造函数(并且可以访问) ,所以它也必须因为这个原因失败。

To answer your specific question:

Does that mean for heap allocated
object size is not necessary?

Class C;//just forward declaration
C * objc = new C();

C++ will not let you do that.

Even if it could let you perform a 'new' on an incomplete type by magically resolving the size at a later time (I could envision this being technically possible with cooperation from the linker), the attempt will fail at compile time because of at least 2 reasons:

  1. operator new can only be used with complete types. From the C++98 standard 5.3.4 - "[the allocated] type shall be a complete object type, but not an abstract class type or array thereof"

  2. the compiler has no idea what constructors exist (and are accessible), so it would also have to fail for that reason.

假情假意假温柔 2024-07-30 08:04:13

对象大小由 new 运算符计算:

Object *o = new Object();

您不需要显式告诉 new 对象大小,但它会计算它(使用 sizeof )运算符)以便在堆上分配正确的空间量。

Object size is computed by the new operator:

Object *o = new Object();

You do not need to explicitly tell new the object size, but it does compute it (using sizeof operator) in order to allocate the correct amount of space on the heap.

蓝咒 2024-07-30 08:04:13

作为一名程序员,您几乎不需要知道 C++ 中对象的大小。 例如:

class A {
    ...  // member data
};

void f() {
    A a;              // allocate on stack
    A * p = new A;    // allocate on heap
}

在这两种情况下,程序员都不需要知道大小 - 编译器当然需要知道它。

请注意,无论您如何创建对象,编译器在创建时都必须知道它的大小:

class B;     // forward declaration - no size:

void f() {
    B b;              // compilation error
    B * p = new B;    // compilation error
}

As a programmer, you almost never need to know the size of an object in C++. For example:

class A {
    ...  // member data
};

void f() {
    A a;              // allocate on stack
    A * p = new A;    // allocate on heap
}

In neither case is knowledge of the size needed by the programmer - the compiler of course needs to know it.

Note that however you create an object, it's size must be known by the compiler at the point of creation:

class B;     // forward declaration - no size:

void f() {
    B b;              // compilation error
    B * p = new B;    // compilation error
}
不寐倦长更 2024-07-30 08:04:13

不可以。为了在堆上分配对象,您必须知道其大小。

Foo *foo=(Foo *)malloc(sizeof(*foo));

No. In order to allocate an object on the heap, you must know the size.

Foo *foo=(Foo *)malloc(sizeof(*foo));
我爱人 2024-07-30 08:04:13

编译器必须查看类的声明有两个原因:它必须知道必须分配的大小(正如其他人已经指出的那样),而且还因为编译器必须知道如何构造对象:它是否有一个隐式的默认构造函数定义了默认构造函数,没有默认构造函数? 即使可以使用无参数构造函数创建对象,编译器也必须知道。

The compiler must see the declaration of the class for two reasons: it must know the size it must allocate (as others have already pointed out) but also because the compiler must know how to construct the object: Does it have a default constructor, implicitly defined default constructor, no default constructor? The compiler must know even if the object can be created with a no argument constructor.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文