Not quite, you have several wrong assumptions in your question.
What has not changed
This is an issue that has not changed program execution 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 there is less memory consumption. Nor is there any worsening or increased memory. There is no malloc
implicit placed by the compiler. There would be no reason to.
Variable declaration and the use of malloc
are orthogonal things. One does not depend on the other. Only the programmer can invoke malloc
, the compiler never does this.
Performance enhancements can be obtained by the compiler independent of it if initial ANSI, C89, C90, C99, C11 or any other standard of the language. Language pattern and compiler version are different things. The compiler is free to do what he wants as long as he follows the language specification. One of these versions of the specification listed.
The fact that you declare a variable before the start of the function or closer to its initial use does not change the program at all. It may change slightly if the assignment is done in addition to the declaration.
What changes
Statement is just a reservation of space for the variable. Note that I said to the variable and not to the object of it.
Stack X heap
Many variables have their object merging with their own storage location. Case of all primary types such as int
, char
, float
, even data sequence as structures and arrays allocated in stack .
Types that use pointers will store the pointer at the location of the variable and will likely allocate the main object to another area, possibly in heap through malloc
(explicitly).
This placeholder in stack is done at the beginning of the function execution. In any version of the specification it is so.
With C99 this has changed a bit and now some stack reservations may occur during the execution of the function, they can be made at the beginning of the scope block. These reservations are made at stack frames . Note that this reservation is not an allocation. The stack is already all in memory. Compiler optimizations may change this stream a bit if it decides it is advantageous.
Theassignmentismadethemomentitappearsinthecode.Iftheassignmentisnowinthemiddleofthefunction,itisclearthattheassignmentwilloccuratthattime.Soiftheassignmentdependsonsomethingdonebefore,therewillbenoproblem.Forthisreason,itisverycommontodaytodeclareandassignavaluetothevariableatthesametime.Beforecompilerswereabletodothis(andsomewerealreadycapableevenbeforethedefaultexistssincestandardsusuallymakewhatcompilersalreadydo)itwascommonfortheprogrammertodeclarethevariableandonlyassignitatatimewhenithadeverythingasheneeded.
Sotherewasnofundamentalchangeinlanguage.Itwasanotherchangeinthewayofcompiling.Thishasonlyhadtheadvantageofbettercodeorganizationsinceitisalwaysbettertodeclarethevariableclosesttoitsuse,andifpossible,inasmallerscope.
Andwiththischangeitwaspossibletotakebetteradvantageofmemory,althoughthishaslittlerelevance.
InyourexamplethememoryreservationismadeinC89atthebeginningofthebeginningofthefunctionexecution.IntheC99example,itisdoneonlyatthebeginningofthefor
block.Thishastheadvantagethatitwillbe"freed" at the very end of for
instead of the end of the function. This space in the stack can be reused by the function just after the end of for
and the variable name can also be reused although this is seldom an interesting thing to do. The advantage of reusing the stack space is rarely relevant.
Conclude
This is a feature introduced to make life easier for the programmer. This is important. With the C99 form the programmer manages the code better, understands better what he is doing, keeps in a smaller region what he really needs to use. Internally, in essence, nothing has changed.