It is common practice to declare stack variables (variables allocated on the excecuting stack rather than dynamically or statically) at the entrypoint of the function rather than mixed with instructions inside the function. See Example 1.a and 1.b. This helps with a better readability and was with older language even required.
With modern C/C++ it is no longer required (..but still good practice).
My Question though: How does the compiler solve it if stack variables are inside a function rather than at the entry point. See Examples 2.a and 2.b how i can imagine he solves it. What is ACTUALLY HAPPENING?
1.a) Example (Common Practice / Best Practice)
void main()
{
int a = 3; // best practice
bool c = false; // best practice
a += 16;
if(a == 5)
{
c=false;
}
}
...rather than ...
1.b) Example (Uncommon)
void main()
{
int a = 3;
a += 16;
bool c = false; // variable after some instructions executed..
if(a == 5)
{
c=false;
}
}
2.a) Possible compiler solution (A)
void main()
{
int a = 3;
a += 16;
bool c = false; // COMPILER CUTS THIS LINE AND MOVES IT UP UNDER "int a = 3;"
if(a == 5)
{
c=false;
}
}
2.B) Possible compiler solution (B)
void main()
{
int a = 3;
a += 16;
{ // COMPILER ADDS SUBSECTION IN ORDER TO INTRODUCE NEW VARIABLES ON STACK
bool c = false;
if(a == 5)
{
c=false;
}
}
}
JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)