在堆上创建对象是否需要对象的大小?
当编译器需要知道 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
不,该列表仅作为示例,并不排除任何情况。 显然,在堆分配中必须知道对象大小,以便可以分配正确数量的内存。
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.
回答您的具体问题:
C++ 不会让你这么做。
即使它可以让您通过稍后神奇地解析大小来对不完整的类型执行“new”(我可以想象,通过链接器的合作,这在技术上是可能的),该尝试也会失败在编译时,至少有两个原因:
operator
new
只能与完整类型一起使用。 根据 C++98 标准 5.3.4 - “[分配的]类型应是完整的对象类型,但不是抽象类类型或其数组”编译器不知道存在哪些构造函数(并且可以访问) ,所以它也必须因为这个原因失败。
To answer your specific question:
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: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"the compiler has no idea what constructors exist (and are accessible), so it would also have to fail for that reason.
对象大小由 new 运算符计算:
您不需要显式告诉 new 对象大小,但它会计算它(使用 sizeof )运算符)以便在堆上分配正确的空间量。
Object size is computed by the
new
operator:You do not need to explicitly tell
new
the object size, but it does compute it (usingsizeof
operator) in order to allocate the correct amount of space on the heap.作为一名程序员,您几乎不需要知道 C++ 中对象的大小。 例如:
在这两种情况下,程序员都不需要知道大小 - 编译器当然需要知道它。
请注意,无论您如何创建对象,编译器在创建时都必须知道它的大小:
As a programmer, you almost never need to know the size of an object in C++. For example:
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:
不可以。为了在堆上分配对象,您必须知道其大小。
No. In order to allocate an object on the heap, you must know the size.
编译器必须查看类的声明有两个原因:它必须知道必须分配的大小(正如其他人已经指出的那样),而且还因为编译器必须知道如何构造对象:它是否有一个隐式的默认构造函数定义了默认构造函数,没有默认构造函数? 即使可以使用无参数构造函数创建对象,编译器也必须知道。
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.