Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maximum number of fields for a C++ object

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++?

like image 390
davetapley Avatar asked Dec 06 '22 01:12

davetapley


2 Answers

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).

like image 119
Steve Jessop Avatar answered Dec 07 '22 15:12

Steve Jessop


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.

like image 38
Stephen Doyle Avatar answered Dec 07 '22 14:12

Stephen Doyle