Andrew Lentvorski wrote: > Declaring everything at the top of a function means that everything > gets allocated on the stack immediately upon function entry and is > held until function exit. No, it really doesn't. It requires that the program behave *as though* that were was is happening, but the specific order of operations is at best loosely defined by the language. Last I checked in the "first allocated, last deallocated" principle is more a convention than an actual requirement.
Even to the extent that it were true, C89 programmers from eon's back have found ways of avoiding allocation when necessary despite the convention. > By declaring variables near first use, you don't allocate/construct > the object until needed and the object can go out of scope and release > its memory sooner (at the end of its block rather than at the end of > the function). > > Of course, modern languages running inside VM's with garbage > collection don't have to care so much. They can delay the allocation > until right before use and they can deallocate as soon as the last > reference disappears. Whether they *do* this is a different > discussion, however. Actually, C++ compilers/runtimes have basically the same option available to them, and a number of them do actually take advantage of it. --Chris -- [email protected] http://www.kernel-panic.org/cgi-bin/mailman/listinfo/kplug-lpsg
