Memory leaks
A program that allocates memory, but doesn't free it, is said to have a
memory leak. For a small amount of data this usually isn't a big problem. However, a program that runs for a long time repeatedly allocating memory without freeing it will eventually crash, often crashing the entire system.
Dangling pointers
Dangling or
stale pointers are another source of problems. These are pointers to memory that has been deallocated. There's no problem in principle with leaving old pointers lying around, as long as they're never used. It's good practice to set pointer to NULL immediately after a
delete so there is no possibility of using them again.
Dynamic allocation expansion policy
There is some (usually a very small percentage of the total cpu time) overhead in dynamically allocating memory. The
policy on how big each expansion should be is important to efficiency. A common policy for array expansion is to double the size each time. One could, in principle, allocate one one extra element each time it the array was expanded, but this would be too inefficient. D Altho it is commonly used, there is no magic in doubling -- it might be more appropriate for a particular application to add a fixed increment.
0 comments:
Post a Comment