C++ 的最大字段数目的

发布于 2024-09-17 05:44:25 字数 196 浏览 8 评论 0原文

这个答案指出在Java中,一个对象可以拥有的最大字段数是65536。C

++中的对象有这样的限制吗?

This answer states that in Java the maximum number of fields an object may have is 65536.

Is there any such limit imposed on an object in C++?

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

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

发布评论

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

评论(3

雄赳赳气昂昂 2024-09-24 05:44:26

C++03 标准,附录 B(实施数量):

  1. 由于计算机是有限的,C++ 的实现不可避免地受到限制
    他们可以做的项目的规模
    成功处理。每一个
    实施应记录那些
    已知的限制。这
    文档可能会引用固定限制
    在它们存在的地方,说明如何计算
    变量限制作为函数
    可用资源,或者说固定的
    限制不存在或未知。

  2. 这些限制可能会限制数量
    包括下面描述的那些或
    其他的。后面括号里的数字
    每个数量建议为
    该数量的最小值。然而,
    这些数量仅供参考
    并且不确定合规性。

该列表包括

  • 对象的大小[262 144]。
  • 单个类、结构或联合中的数据成员 [16 384]。
  • 在单个类中声明的成员 [4 096]。

因此,没有定义的限制,但是应用限制的实现“应该”使限制至少与指示的值一样大。恐怕我不知道常见的实现实际上是做什么的,但如果他们不记录它,他们要么不合规,要么限制是“未知的”。我想“未知”通常意味着“在编译时可以容纳尽可能多的可用内存”。

顺便说一句,我不确定“类中的成员”和“类中声明的成员”之间有什么区别。我认为这意味着如果你的基类有10个数据成员,并且你的类声明了10个成员,那么你的类总共有20(或21)个数据成员(取决于基类子对象是否算作数据成员) )。

C++03 standard, Annex B (implementation quantities):

  1. Because computers are finite, C++ implementations are inevitably limited
    in the size of the programs they can
    successfully process. Every
    implementation shall document those
    limitations where known. This
    documentation may cite fixed limits
    where they exist, say how to compute
    variable limits as a function of
    available resources, or say that fixed
    limits do not exist or are unknown.

  2. The limits may constrain quantities
    that include those described below or
    others. The bracketed number following
    each quantity is recommended as the
    minimum for that quantity. However,
    these quantities are only guidelines
    and do not determine compliance.

The list includes

  • Size of an object [262 144].
  • Data members in a single class, structure, or union [16 384].
  • Members declared in a single class [4 096].

So there's no defined limit, but an implementation which applies a limit "should" make the limit at least as big as the value indicated. I'm afraid I don't know what common implementations actually do, but if they don't document it they're either not compliant, or else the limit is "unknown". I guess that "unknown" generally means, "as many as we can fit in the available memory at compile time".

Btw, I'm not sure what the difference is between "members in a class" and "members declared in a class". I think it means that if your base class has 10 data members, and your class declares 10 members, then your class has 20 (or 21) data members in total (depending whether the base class sub-object counts as a data member or not).

〃安静 2024-09-24 05:44:26

我不相信 C++ 规范中有任何内容可以涵盖这一点,但我怀疑不同的编译器会有不同的限制。

I don't believe that there is anything in the C++ spec to cover this, but I suspect that different compilers will have different limits.

離人涙 2024-09-24 05:44:26

对象可以拥有的字段数量没有硬性限制,但我认为这高度依赖于平台和编译器。

另外,如果您在一个对象中使用甚至 100 个或更多字段,那么您的类的设计可能存在严重错误,因此不必担心限制,而应该担心 OOP 设计

There is no hard limit on the amount a fields an object can have, but saying that I imagine this is highly platform and compiler dependent.

Also there is probably something very wrong with the design of your class if you are using even 100 or more fields in an object, so shouldn't have to worry about limits instead worry about OOP design

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