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?
A local variable, however, has a limited scope: it exists only within the block that it is declared in. Once that block ends, the variable is destroyed and its values lost . A local variable of the same name declared elsewhere is a different variable.
PHP has three different variable scopes: local. global.
A scope is a region of the program and broadly speaking there are three places, where variables can be declared: Inside a function or a block which is called local variables, In the definition of function parameters which is called formal parameters.
There are four types of scope: file scope, block scope, function scope and.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With