Where is it better to put the initialization code, before the loop or inside it?

Sorry if this is a stupid question :-)

Background

I have legacy code that looks like this:

struct { int field1; int field2; int field3; int field4; ... many many fields } myStruct; while (something) { initialzationFunction(&myStruct); // ...change fields of myStruct and do stuff. } 

Each iteration of the while loop requires myStruct to be initialized with something, let's say zero. initialzationFunction initializes all myStruct fields to zero.

Question

Is it good to keep the initialzationFunction inside the while loop, or is it better to call it once before the loop, and let the programmers initialize what they need "manually" if they can change this code.

edit: Unfortunately, myStruct is a global variable, so an automatic variable is not an option if I don't want to pass it as a parameter to a lot of obsolete functions that use it.

What I think

  • just calling initialzationFunction () will prevent errors in case someone changes the code and forgets to initialize myStruct later.
  • It may be more informative to know which specific fields are initialized.
  • If only a few fields are changed later in the while loop, calling the initialzationFunction () function, which contains all the fields, is redundant.

What would you do?

+8
c legacy-code
source share
5 answers

Since the fields of the structure change inside the while loop, it makes sense to initialize it during each iteration for any processing purpose performed in the loop.

I would say that it is normal to reinitialize multiple fields, even if there are no changes in the loop. But keeping track of field changes and excluding these fields when you initialize the next next iteration, there will be a problem that you can get around.

An alternative would be to use the temporary struce variable with initialization values ​​and simply assign it at the beginning of each iteration.

+5
source share

If you leave the code to others to support it, and the code is not a trusted access point, initialize each time, as there will be fewer errors introduced by others.

If the code is a verified critical access point, then initialize once and then clear the code.

Premature optimization is the root of all evil

+6
source share

If the structure needs to be initialized every time the loop is executed, then doing it inside is fine. You can also use a dummy structure that you initialize before the loop, and use memcpy to copy to the cleaned structure into the real one inside the loop.

Or as in the accepted answer to a question related to Steve Jessop, instead of using memcpy just use the normal assignment and let the compiler worry about copying.

+1
source share

Well, ideally, you would like to perform the minimum number of operations that solve the problem that you are solving. Following this logic, it would be better to leave the initializationFunction out of the loop and just update the fields needed to iterate the loop.

From a maintenance point of view, if the algorithm in your loop can break (or behave strangely), if someone forgot to reset the member from your struct object, then it would be better to initialize everything in each loop, However, this does not exclude the possibility of future errors, this only makes it less likely. In the end, it all depends on the level of competence of the attendant.

In terms of performance, this is micro-optimization , and it doesn't really matter (unless you do something a lot of time in the initialization function).

+1
source share

This is a matter of balance and complexity. If most of the members in the structure never access the while loop, initialization is clearly redundant ... BUT THAT, why are they sitting together inside the structure? What was their original purpose? In this case, the code itself is more complex than necessary, although unoccupied data in C is, of course, less confusing than never-executable code.

If OTOH the main part of the structure elements is used in the while loop, then adding a simple zero assignment to each of them will not hurt much, because each subsequent operation on this member more or less mitigates the impact on 1 / n initialization performance.

What I see as harmful to the maintenance of the code is that the init function itself needs to know the structure, which means that you scatter the information in more places than necessary. IIRC C allows memset attributes to be structured (touch the structure as an unsigned char vector), and the participants will really come out 0 ==> if this is a blatant right, then I am very sorry and someone can break the printed version of all the standards above my head.

+1
source share

All Articles