Review

On the operating system level, memory management consists of keeping an account of what process owns what memory block and allocating more memory to a process when requested (thus the operating system can determine when a process is trying to access a memory location that is not its own - and terminate the process for faulty memory access). On the process level, memory management consists of keeping account of free segments or reclaiming those segments that the process no longer needs.

Statically allocated memory for a process is the memory into which the program is loaded prior to its execution. It does not change throughout the program's execution, and its layout is completely determined by the compiler. Both the data and the instructions of the program are stored in the statically allocated memory.

It is clear that many programs require more memory to store data or possibly additional instructions. These requests for additional memory allocation during the execution of a program - and the manner in which they are served - are generally referred to as "dynamic memory allocation".

In modern operating systems, memory management is a two- tier affair with a single operating system memory manager and different process memory managers specific for each running process. The operating system manager allocates rather big " chunks " (blocks) of memory to the process memory managers. A process memory manager divides these blocks into suitable memory segments, which it allocates to its process. For the process memory manager, dynamic memory allocation includes deciding which segment to allocate (if need be, divide a bigger segment into two smaller ones) and removing it from the list of free segments. The fragmentation caused by this division is inevitable and may degrade program performance. Therefore, deallocation includes (besides adding the freed segment to the list of free segments) possibly joining the freed segment with adjacent free segments to counter somewhat the fragmentation.

When a request for more memory is issued by a program (through a malloc() , calloc () ,or realloc() call), its process memory manager allocates it a segment of a size equal to or bigger than the size requested (if such a segment is not ready, the process manager creates it from a bigger one; if this is not possible, the process memory manager requests a new block from the operating system manager). If there is not enough memory, the allocators return NULL ; otherwise , they return the address of the allocated segment. The contents of the allocated segment are arbitrary for malloc() . All bits are cleared for calloc() and are preserved up to the minimum of the old size and the new size for realloc() (in the extended/reduced part, the contents are arbitrary). Allocation fails for three reasons only: not enough memory is available, the requested size exceeds the limit, or the process memory manager is corrupted.

The deallocator free() returns a segment to the list of free segments that is kept by the process memory manager. The functions realloc() and free() can each cause problems (including corruption of the process memory manager) if the pointer supplied is not pointing to a segment previously allocated by malloc() , calloc() ,or realloc() , or if the segment it points to has meanwhile been freed.

There is not much to do about memory allocation errors, and a program should be promptly terminated if it runs into such an error. Detailed information (e.g., a log entry) about the error and its localization within the program should be provided so that the nature of the problem can be determined later. Safety-critical and fault-tolerant software systems should avoid dynamic memory allocation in their critical sections, possibly do their own memory management, or even be able to work with disk (instead of main) memory if allocation errors occur during execution of a critical section.



Memory as a Programming Concept in C and C++
Memory as a Programming Concept in C and C++
ISBN: 0521520436
EAN: 2147483647
Year: 2003
Pages: 64

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net