My question is pretty simple: how many variables can be in local scope to be properly translated?
I have to create a small translator (for studying purposes) from C++ to Assembly. During the translation process, there is a dynamic table of identifiers (variable names, in simple case, I suppose). How many can there be?
I mean, my table is dynamic anyway as well, but I need to create an array of tokens where each has 2 numbers - a table ID and a record ID in the table. So I want to know, which type should these IDs be - int
, short
, long
, etc?
How many variables can be in local scope
The C++ standard does not specify an exact maximum number.
It does have following recommendation (quote from latest standard draft):
[implimits]
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.
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.
- Identifiers with block scope declared in one block ([basic.scope.block]) [1'024].
Someone wrote a test for this, and commonly used compilers appear to support at least 8k: https://github.com/fritzone/cpp-stresstest