Reasons to place C / C ++ variables in an unnamed area?

Possible duplicate:
Can I use blocks to control the scope of variables in C ++?

I came across some C ++ code that looked like:

int main(void) { int foo; float qux; /* do some stuff */ { int bar; bar = foo * foo; qux = some_func(bar); } /* continue doing some more stuff */ } 

Initially, I thought that perhaps the original author used braces to group some related variables, but since the system under development does not have a lot of memory, I thought that the author might have intended to allow the panel scale and any variables that go, but do not have them around the entire covering area (foo).

Is there a reason for this? It seems to me that this is not necessary and that any modern compiler makes it unnecessary?

+4
source share
6 answers

It seems to me that this is not necessary and that any modern compiler makes it unnecessary?

Yes, modern compilers optimize memory usage in such cases. An extra area will not make the code faster or more memory efficient.

However, they cannot optimize objects with destructors with side effects, since this will change the behavior of the program. Thus, it makes sense to do for such objects.

Is there a reason for this?

It is useful to group related code together. You know that variables declared inside curly braces will not be used anywhere, which is very useful to know.

+7
source

In the case of C / C ++, you can try to limit conflicts between names (a function that is so long that it requires a single scope variable is thus a bad idea ...), i.e. if in the same function than viewing them in this way will make sure that they do not collide / do not redefine each other.

Usually the function of the internal scope does not affect the size of the stack distribution - the stack is pre-allocated for all local variables regardless of the scope.

+4
source

If the code is really as you showed, it is probably pretty pointless. Most compilers I've seen allocate space for all local variables when entering a function and free it when leaving a function. However, there are several other possibilities.

If what you showed as bar was an object of some type of class (especially something with a destructor), the destructor will work when you exit the area, although the space was not released until a later time.

Another possibility is that there were actually two internal areas:

 int main() { // ... { // some variables } // ... { // other variables } } 

In this case, the space for local variables will be allocated when entering main - but, some variables and other variables will (usually) share the same space. That is, the allocated space will be enough to accommodate the larger of the two, but it will not (usually) be the sum of the two, as you would use if you defined all the variables in the main .

+3
source

If you do this several times in one method, this can lead to the fact that this method takes up less space on the stack, depending on your compiler. If you are limited in resources, you can be on a microcontroller, and their compilers are not always full-featured x86 compilers.

In addition, if you do this with full classes (instead of ints and floats), it allows you to control where the destructor is called.

 class MyClass; int main(void) { int foo; float qux; /* do some stuff */ { MyClass bar; qux = some_func(bar); } // <-- ~MyClass() called here. /* continue doing some more stuff */ } 
+3
source

intent to allow scope and any variables with go and not have them around for everything encompassing (foo's)

This may be one (insignificant and hereditary) reason.
Another reason is to tell the reader that the int bar used only in this area in order to have very little functionality (the kind of function inside the function). After that bar is not used.

Your code is equivalent to:

  inline void Update (int &foo, float &qux) { int bar = foo * foo; qux = some_func(bar); } int main () { ... Update(foo, qux); ... } 

Most compilers optimize the call to Update() inside main() and embed it, which generates the same as what you placed.

+2
source

Most likely, this will help the programmer, and not optimize the output, since the modern compiler is certainly smart enough that the temporary variable is used only once.

On the other hand, for the programmer, he adds a logical separation, which means that "these variables are necessary only for this code," and perhaps also "this code does not affect other code in this function."

+2
source

Source: https://habr.com/ru/post/1443768/


All Articles