in Education by
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)

1 Answer

0 votes
by
When, how and where the compiler instantiates or allocates such variables is entirely implementation dependent. It may create a stack frame for all variables at the start of the function, or it may extend the stack frame at the point of declaration, or it may not use the stack at all and use register storage instead. The language only requires that the correct semantics are implemented; that the variable is not in scope (i.e. cannot be referenced by code) before it is declared, and that the scope ends after the closing } of the block in which it was declared.

Related questions

0 votes
    I have a struct X with two 64-bit integer members, and a constructor: struct X { X(uint64_t a, ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 16, 2022 in Education by JackTerrance
0 votes
    I am trying to understand the usage of private const in the class. My understanding is that private const is used to make ... (void) { int const MyExample::x = 3; std::cout...
asked Apr 12, 2022 in Education by JackTerrance
0 votes
    I have a struct X with two 64-bit integer members, and a constructor: struct X { X(uint64_t a, ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 12, 2022 in Education by JackTerrance
0 votes
    I have a struct X with two 64-bit integer members, and a constructor: struct X { X(uint64_t a, ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 12, 2022 in Education by JackTerrance
0 votes
    I have a struct X with two 64-bit integer members, and a constructor: struct X { X(uint64_t a, ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 12, 2022 in Education by JackTerrance
0 votes
    I have a struct X with two 64-bit integer members, and a constructor: struct X { X(uint64_t a, ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 10, 2022 in Education by JackTerrance
0 votes
    I have defined an interface in C++, i.e. a class containing only pure virtual functions. I want ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 13, 2022 in Education by JackTerrance
0 votes
    I have defined an interface in C++, i.e. a class containing only pure virtual functions. I want ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 13, 2022 in Education by JackTerrance
0 votes
    I have defined an interface in C++, i.e. a class containing only pure virtual functions. I want ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 13, 2022 in Education by JackTerrance
0 votes
    When compiling boost filesystem (1_46_1) with Intel 12 Release 4, and Visual Studio 10, I get this error ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 2, 2022 in Education by JackTerrance
0 votes
    What does a compiler do? It translates source code into activities (which may comprise groups of machine instructions) ... only enables the programmer to edit the program files....
asked May 1, 2021 in Education by JackTerrance
0 votes
    I am trying to implement the Josephus election problem using an array as a mock circular linkedlist. The item and next arrays represent ... array x = next[x]; count++; } std::cout...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    I am working on a very large scale computing library that is using STL heavily. The library is being ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 12, 2022 in Education by JackTerrance
0 votes
    Which of the following terms refers to the rotational motion of a robot arm? (a) swivel (b) axle (c) retrograde (d) roll Please answer the above question....
asked Oct 22, 2022 in Education by JackTerrance
0 votes
    The number of moveable joints in the base, the arm, and the end effectors of the robot determines_________ ( ... limits (d) flexibility Please answer the above question....
asked Oct 22, 2022 in Education by JackTerrance
...