recv , like read and other similar functions, do not care about the previous contents of the buffer, it uses it only to write the result.
Not that it would matter in any case: since you are not initializing your buffer, its contents will be "undefined" even if you declare the variable as local to the loop.
Also, on most C implementations:
- not initializing this variable means that everything that happens on the stack in this place will happen, which, in turn, means that it will be in the same place as in the previous iteration, which gives you the exact result as having a variable outside the loop.
- Stacking is cheap - in general, they just require a register setting;
- in fact, they are even cheaper: usually register adjustment is performed only at the beginning of a function that takes into account all local variables; the scope of a local variable becomes just a compilation time construct, since it is allocated when the function starts.
Obviously, instead, if you initialized your variable, it would be different - the code to complete the initialization had to be run at each iteration; but, as said above, there is no need to initialize anything, recv just doesn't care about the current state of the buffer.
source share