Not quite so, has several wrong assumptions in the question.
What hasn’t changed
This is an issue that has not changed the implementation of the programme at all. This change is purely a new way for the compiler to work, but it does not generate different code because of this.
There is no improvement in performance and no less memory consumption. So little is there worsening or memory increase. There is no malloc()
implicit placed by the compiler. Nor would there be any reason to have.
Variable declaration and the use of malloc()
are orthogonal things, one does not depend on the other. Only the programmer can invoke the malloc()
, compiler never does this.
Performance improvements can be obtained by the compiler independent of it being initial ANSI, C89, C90, C99, C11, C18 or any other language standard. Standard language and compiler version are different things. The compiler is free to do whatever he wants as long as he follows the language specification (one of the ones listed above).
The fact that you declare a variable earlier at the start of the function or closer to its initial use does not change the program at all. It may change if in addition to the statement the attribution is also made, but then the attribution changes, in some cases.
What changes
Declaration is only a reserve of space for the variable. Note that I said for the variable and not for her object.
Stack X heap
Many variables have their object confused with its own storage location. Case of all primary types like int
, char
, float
, even sequence of data such as structures and arrays allocated in the stack.
Types that use pointers store the pointer at the location of the variable and will likely allocate the main object in another area, possibly in the heap through the malloc()
(explicitly).
This space reserved in stack is done right at the start of the function execution. In every C compiler it is like this, no matter what version of the specification it is compatible, because the specification doesn’t say anything about it, but it is something that in practice is like this.
Note that in C99 the reservation is still made at the beginning of the function execution (this is implementation detail, but all compilers act like this). With C99 only slightly changed the syntax allowing the statements can be made outside the beginning of the scope block, but it’s only syntactic change. These reservations are made on stack frames. Note that this reservation is not an allocation. O stack is already all in memory. Compiler optimizations can change this flow a little if he decides it is advantageous.
The assignment is made at the moment it appears in the code. If now the assignment is in the middle of the function, it is clear that the assignment will occur at that time. So if the attribution depends on something done before there will be no problem. Therefore today it is very common to declare and assign a value to the variable at the same time. Before compilers were able to do this (and some were able even before the standard existed since standards usually make official what compilers already do) it was common for the programmer to declare the variable and only assign at the moment ahead when he had everything as he needed to do this, thus saving a memory access
So there was no fundamental change in language. It was another change in the way of compiling, bringing only the advantage of better organization of the code since it is always better to declare the variable closest to its use, and if possible, in a smaller scope.
And with this change it was possible to take better advantage of memory, although this has little relevance.
In your example the memory reserve is made in C89 right at the beginning of the start of the execution of the function. In the example of C99 it is done only at the beginning of the for
. This brings the advantage that she will be "released" at the very end of for
instead of the end of the function. This stack can be reused by the function just after the end of the for
and the variable name can also be reused although this is rarely something interesting to do. The advantage of reusing the space of the stack rarely is anything relevant.
Completion
This is a feature introduced to make life easier for the programmer. This is what’s important. With the form C99 the programmer better manages the code, better understands what he is doing, keeps in a smaller region what he really needs to use. Internally, in essence, nothing has changed.