Fred Rogers
Fred Rogers

Reputation: 91

Code blocks in the middle of a function or code

whats the point and what does it mean to have code blocks in the middle of a function or program like the example below

#include <iostream>
#include <string>
using namespace std;

int main(){
    int x = 32;

    { // random code block starts here

        if (34 > x){
            cout <<"x greater"<<endl;
        }else cout << "no\n";

    }// ends here

    return 0;

}

Upvotes: 2

Views: 975

Answers (4)

2785528
2785528

Reputation: 5566

what does it mean to have code blocks in the middle of a function or program

Braces are used to control scope. The opening brace creates a new and smaller scope. The closing brace ends the new and smaller scope.

If there were auto vars declared in the smaller scope, the auto vars cease to exist at the closing brace. Which frees up the variable name for re-use in the next scope.


Though nothing special happens in your code sample, something special can happen at a closing brace. The end of scope can also free auto-memory vars, including invoking the dtor of user define objects (class or structs).


In the following, block 1 (named 'using auto-promotion') shows a way to use auto-promotion to create the byte order required in the value 'result' and cout for the user to inspect.

In block 2 (using static_cast), achieves the same, and note that it happens to re-use the declared auto variable with the name 'result'. No collision occurs with the 1st block auto var, because the closing brace ended the scope for the name 'result' of block 1.

Block 3 (using static cast function of my own creation) does the same thing ... declares 'result' a 3rd time, again with no collision.

int exec()
  {
     uint8_t byte0 = 0x00;
     uint8_t byte1 = 0xAA;
     uint8_t byte2 = 0x00;
     uint8_t byte3 = 0xAA;

     uint16_t hword0 = 0xAA00;
     //uint16_t hword1 = 0xAAAA; not used

     // using auto-promotion
     {
        uint64_t b4567 = hword0; // auto promotion
        uint64_t b3    = byte3;
        uint64_t b2    = byte2;
        uint64_t b1    = byte1;
        uint64_t b0    = byte0;

        uint64_t result =  (
           (b4567 << 32) |
           (b3    << 24) |
           (b2    << 16) |
           (b1    <<  8) |
           (b0    <<  0)   );

        cout << "\n  " << hex << result << endl;
     }

     // using static cast
     {
        uint64_t result = (
           (static_cast<uint64_t>(hword0) << 32) |
           (static_cast<uint64_t>(byte3)  << 24) |
           (static_cast<uint64_t>(byte2)  << 16) |
           (static_cast<uint64_t>(byte1)  <<  8) |
           (static_cast<uint64_t>(byte0)  << 0 )
           );

        cout << "\n  " << hex << result << endl;
     }

     // using static cast function
     {
        uint64_t result = (
           (sc(hword0) << 32) |
           (sc(byte3)  << 24) |
           (sc(byte2)  << 16) |
           (sc(byte1)  <<  8) |
           (sc(byte0)  << 0 )
           );

        cout << "\n  " << hex << result << endl;
     }

     return 0;
  }

One can capture auto-memory and release it by controlling the scope of inner auto vars. This memory can even be re-used with a different name.

The code blocks (for smaller scopes) allow the programmer to use auto-memory differently.


The function 'sc' is designed to reduce typing.

//          vvvvvvvv ---- formal parameter allows auto-promotion
uint64_t sc(uint64_t ui64) {
  return static_cast<uint64_t>(ui64); // static cast
}

Upvotes: 0

Alex
Alex

Reputation: 33

As far as I know, this creates a new scope so any objects or variables declared within the {} will only be available there. This can be especially useful for creating instances of objects as the destructor for the object will be called when it goes out of scope.

In this case, however, there is no need for the {} as there are no variables declared or objects created.

Upvotes: 2

Ranjan Kumar
Ranjan Kumar

Reputation: 1210

code block can be used to restrict the scope of variables declared in block.           int main(){
    int x = 32;

    { // random code block starts here
        int y = 5;
        if (34 > x){
            cout <<"x greater"<<endl;
        }else cout << y\n";

    }// ends here

    return 0;

}

Upvotes: 0

Alexey Frunze
Alexey Frunze

Reputation: 62106

Using the block is completely unnecessary in this particular case.

Upvotes: 0

Related Questions