Dynamic Memory Allocation
Published on Sep 21, 2019
In computer science dynamic memory allocation is the allocation of memory storage for use in a computer program during the runtime of that program. It is a way of distributing ownership of limited memory resources among many pieces of data and code.
A dynamically allocated object remains allocated until it is deallocated explicitly, either by the programmer or by a garbage collector this is notably different from automatic and static memory allocation. It is said that such an object has dynamic lifetime.
The problem of fulfilling an allocation request, which involves finding a block of unused memory of a certain size in the heap, is a difficult problem. A wide variety of solutions have been proposed, including:
3)Buddy memory allocation
The main problem for most dynamic memory allocation algorithms is to avoid both internal and external fragmentation while keeping both allocation and deallocation efficient. Also, most algorithms in use have the problem that a large number of small allocations can cause wasted space due to collecting metadata; thus most programmers avoid this, sometimes by using a strategy called chunking.
How Does Dynamic Memory Allocation 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, we imagine that all the available memory is one large array to be used as a global pool:
We use gray shading to 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" . As it happens, in C, all strings must be terminated by a special character called `NULL' 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 NULL. Thus he requests 4 bytes by calling malloc(4) .
Suppose he is given the first 4 bytes from the pool; then memory would now look like this:
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 shaded gray.
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. C++ provides two dynamic allocation operators: new and delete .
These operators are used to allocate and free memory at run time. Dynamic allocation is an important partof almost all real-world programs. As explained in Part One, C++ also supportsdynamic memory allocation functions, called malloc() and free().
These are includedfor the sake of compatibility with C. However, for C++ code, you should use the new and delete operators because they have several advantages.The new operator allocates memory and returns a pointer to the start of it. The delete operator frees memory previously allocated using new.