Search code examples
c++while-loopallocationvariable-declarationvalue-initialization

How many times are primitive data types allocated inside loops?


Here's an example:

while (i < 10)
    {
        int j = 1;
        string k = "hello.";
    }

j is a primitive data type and k is an object. According to Do built-in types have default constructors?,

So non-class types (including fundamental types, array types, reference types, pointer types, and enum types) do not have constructors.

And according to Declaring Variables inside Loops, good practice or bad practice? (2 Parter),

the constructor and destructor must be executed at each iteration in the case of an object (such as std::string).

However, variable declaration within the while loop C/C++ says,

while(i--)
    {
      int i=100; // gets created every time the loop is entered
      i--;
      printf("%d..",i);
    } // the i in the loop keeps getting destroyed here

I've learned that when a function is called (such as main() by the operating system), all local variables are created at the start of the function and destroyed at the end of the function. The above while loop quote is saying that the primitive data type i is being created when it is declared in the while loop block and destroyed at the end of the while loop block for each iteration.

Is this really true? #1 Are primitive data types declared in the while loop block being allocated for each iteration of the while loop? #2 Am I wrong to assume that these declarations in a while loop should be created at the start of the function the while loop block is contained in?

I'm looking for a detailed explanation, not just yes or no.


Solution

  • Logically, yes, the variables are being created at the start of the loop body for every iteration of the loop, and destroyed at the end.

    In your first example, the constructor of std::string (actually std::basic_string<char> which is what std::string is) will be invoked for every iteration, as will the destructor. Similarly j is being created and set to 1 for every iteration.

    In the second example, i will be printed with the value 99 for the same reason.

    Compilers do have some leeway in this. If they can detect there is no effect of repeatedly creating and destroying, they can keep the variable alive, and simply reinitialise it for every loop iteration. They can even eliminate variables completely (e.g. just print 99 repeatedly, and not create the variable). However, such things cannot be relied on, and a program cannot test to see if it happens.