I have stressed so far the advantage of dynamic memory allocation. Now let me mention its two main disadvantages or dangers.
Freeing Memory
The user is responsible for freeing up memory when he is finished with it. This is a serious responsibility, a potential source of bugs that are very hard to find. For example, suppose you free up a location before you're actually finished with it. Then further access to the location will either cause a run-time error (memory violation) or, what is worse (but more common), you will get access to a location that is being used for some completely unrelated purpose. Trouble also occurs if you forget to free up some space. If you do this, you are losing the advantages of dynamic allocation. And in some circumstances - e.g. if you were writing the program that manages the space on a disk drive - this could be disastrous. There are no surefire safeguards against these problems, you must be very careful when writing programs to return memory when you are finished with it, but not before.
Fragmentation
of Memory As the preceding example demonstrated, with dynamic allocation the `free' parts of memory are not all together in one contiguous block. This is called fragmentation of memory, and it can grow into a very serious problem: it is possible to have a large amount of free memory but for it to be broken up into such tiny fragments that there is not enough contiguous free space to store another name. Suppose that after using dynamic allocation for a while memory becomes fragmented and we need to obtain a block of some size. If only the remaining free blocks were contiguous, we'd have enough room, but, in the present configuration, it looks like we are doomed. There are several possible solutions to this problem. The one we will study is based on the insight that a large data structure does not necessarily have to be allocated in one single contiguous chunk of memory. Instead it might be decomposed into smaller components that are somehow linked together in a chain. Each component in this chain can very well be allocated in a different region of the global pool. Thus, when using linked data structures, fragmentation becomes much less of a problem.
BACK
HOME
NEXT
M.kirthi Kumar Reddy
Computer Science Department
CS 561, CWID:10436087
Illinois Institue of Technology
Chicago,Illinois,60616