HOW DOES IT WORK?


The key idea is to regard all the available memory as a large global pool that can be used for any purpose whatsoever. When you need memory, you ask for just the amount you need; it is given to you from the global pool and is marked as being no longer `free' so that a subsequent request for memory does not allocate this same block again for a different purpose. Memory allocation is done by some sort of procedure call; in C, malloc(n) allocates a block of memory of size n bytes. It is your responsibility to return memory to the global pool when you are finished with it; when you do so it will be marked `free' again and be available for other uses (you might get it again the next time you request some memory). To continue our class-names example, we imagine that all the available memory is one large array to be used as a global pool as we can see in figure 1:

FIGURE 1



The unnamed parts indicate the regions of memory that belong to the global pool, i.e. which are free. At present, all of it is free. Now, if the user needs memory to store the name JOE, he must requests enough space to store the string "JOE". Let us consider our C language, in which all strings must be terminated by a special character called `NUL' whose code is 0 and which is written \0. Therefore, the user needs 3 bytes to store J, O, and E, plus an additional byte for NUL. Thus he requests 4 bytes by calling malloc(4). Suppose we are given the first 4 bytes from the pool; then memory would now look like this:

FIGURE 2



The memory allocator keeps track of what memory is free and what memory has been allocated. It now knows that the 4 bytes which it gave to the user are no longer free; they are no longer in the global pool. This we represent visually by the fact that these bytes are no longer unnamed. The question marks now visible indicate that we do not know in what state the bits of this block of memory really are. A memory allocator typically makes no guarantee about this issue. The block of memory which is allocated and returned to the user must be presumed to contain garbage. It is the responsibility of the user to then initialize this block properly so that its contents become meaningful. In the present case, the user simply copies the string "JOE" into it:

FIGURE 3



You can see that it takes up exactly the space that is needed for it. The rest of memory can be used to store the other names. Suppose the next name is MARYJANE: 8 characters + 1 NUL. The user asks for 9 bytes and copies the string into them. You must be wondering what the number’s 4 and 9 are doing which I will discuss in next few minutes. Before that I woud like to discuss why this new block is not contiguous with the previous one. The reason is that, in general, it won't be. This, mainly for two reasons: * The memory allocator might very well decide to allocate the next block from some other part of memory. * Often, the memory allocator needs to allocate a little bit more than the user requested so that it can store, just before the user's block, some information about it, such as its size. Thus, when the user wants to `free' the block, the memory allocator can find out how big it is and return the correct number of memory locations to the global pool.

FIGURE 4






BACK
HOME
NEXT
M.kirthi Kumar Reddy
Computer Science Department
CS 561, CWID:10436087
Illinois Institue of Technology
Chicago,Illinois,60616