One of the most important language oriented functions of programming this support of service for supervising memory and objects which are stored in memory.
Language C provides three excellent ways to assign memory for objects:
Memory static allocation: the place for the object is given in a set from two subjects; these objects have a degree (or the whole life) while the set from two subjects which contains them, is loaded in memory.
Automatic memory allocation: time objects can be stored on a stack, and this place is automatically released for reuse after they quit the block in which they are declared
Memory dynamic allocation: memories in any size it is possible to demand at usage of functions of library, such as malloc from memory area; these blocks are saved while the place for a reuse subsequently is not released, naming library function free
These three approaches are corresponding in various situations and have various exchanges. For example, static allocation of memory does not have any allocation, the upper automatic allocation can involve small quantity upper, and memory dynamic allocation can have potentially much above both for allocation, and for clearing. On the other hand, the stack place typically much more is more limited in transient or than static memory, or than a heap place, and memory dynamic allocation allows to arrange the objects which size is known only in execution time. The majority of programs C does the wide use of all of three.
Automatic or static allocation is usually preferred, because storage the compiler controls, releasing the programmer potentially from economic job of hand-held allocation subject to errors and storage release. However, the majority of data structures can grow in the size in execution time and as for static allocations (and automatic allocations in C89 and C90) should be the motionless size, is many situations in which dynamic allocation should be used.
Automatically and dynamically assigned objects only are calibrated, if calibrating is obviously defined; otherwise they originally have indefinite values. If the program tries to get access to not calibrated value, results can be indefinite. The majority of modern compilers try to detect and warn about this problem, but also false positive sides and false negations occur.
Other problem - that the heap memory allocation should be manually clocked with its actual usage in any program to be again used in the greatest possible degree.
On the contrary, there is a possibility to release memory too soon and to continue to get access to it; however, as the allocation system can reallocate or is direct use the released memory, unpredictable behaviour, possibly, will occur, when multiple users will corrupt data each other. As a rule, tags will appear regarding the program far remote from an actual error. Such problems are improved in languages with an automatic collection of garbage or RAII.
|