Dynamic Memory Allocation in C
Dynamic Memory Allocation in C
We have only had as much memory available as we declared for our variables, having the size of all of them to be determined in the source code, before the execution of the program. But, what if we need a variable amount of memory that can only be determined during runtime? For example, in the case that we need some user input to determine the necessary amount of memory space.The answer is dynamic memory, for which C++ integrates the operators new and delete. It means a program can obtain memory at runtime
Advantages
1.Avoid memory wastage 2.Prevents underflow and overflow to an extent
Disadvantages
1.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. 2.Fragmentation of Memory 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.
Application.
1.Database management systems