Memory Leakage:
Arising due to having lost the pointer to some memory that is allocated on heap.in this case the memory remains allocated and can't be freed because it's base address is lost.it leads to substantial wastage of the free storage on the heap.
int *p=new int[1000];
----
----
delete []p;
4bytes
------------
4000bytes
if delete p
only first 4 bytes freed which results in memory leakage of 3996 bytes
Note:Also it is very important to note that whenever we have allocated memory for more than one element i.e using subscript notation [size],it becomes necessary to de-allocate the memory by using the subscript notation [] in the delete operator as shown above.if this is not done and a delete statement without a pair of
[ ] is used,then only the first element whose address is contained in the pointer is deallocated resulting in the problem of memory leakage.
Memory leak
Memory leaks can be really annoying. The following list describes some scenarios that result in memory leaks.
Arising due to having lost the pointer to some memory that is allocated on heap.in this case the memory remains allocated and can't be freed because it's base address is lost.it leads to substantial wastage of the free storage on the heap.
int *p=new int[1000];
----
----
delete []p;
4bytes
------------
4000bytes
if delete p
only first 4 bytes freed which results in memory leakage of 3996 bytes
Note:Also it is very important to note that whenever we have allocated memory for more than one element i.e using subscript notation [size],it becomes necessary to de-allocate the memory by using the subscript notation [] in the delete operator as shown above.if this is not done and a delete statement without a pair of
[ ] is used,then only the first element whose address is contained in the pointer is deallocated resulting in the problem of memory leakage.
Memory leak
Memory leaks can be really annoying. The following list describes some scenarios that result in memory leaks.
- Reassignment I'll use an example to explain reassignment.
char *memoryArea = malloc(10);
char *newArea = malloc(10);
This assigns values to the memory locations shown in Figure 4 below.
Figure 4. Memory locationsmemoryAreaandnewAreahave been allocated 10 bytes each and their respective contents are shown in Figure 4. If somebody executes the statement shown below (pointer reassignment )…memoryArea = newArea;
then it will surely take you into tough times in the later stages of this module development.In the code statement above, the developer has assigned thememoryAreapointer to thenewAreapointer. As a result, the memory location to whichmemoryAreawas pointing to earlier becomes an orphan, as shown in Figure 5 below. It cannot be freed, as there is no reference to this location. This will result in a memory leak of 10 bytes.
Figure 5. Memory leak
Before assigning the pointers, make sure memory locations are not becoming orphaned. - Freeing the parent block first Suppose there is a pointer
memoryAreapointing to a memory location of 10 bytes. The third byte of this memory location further points to some other dynamically allocated memory location of 10 bytes, as shown in Figure 6.
Figure 6. Dynamically allocated memoryfree(memoryArea)
IfmemoryAreais freed by making a call to free, then as a result thenewAreapointer also will become invalid. The memory location to whichnewAreawas pointing cannot be freed, as there is no pointer left pointing to that location. In other words, the memory location pointed bynewAreabecomes an orphan and results in memory leak.
Whenever freeing the structured element, which in turn contains the pointer to dynamically allocated memory location, first traverse to the child memory location (newAreain the example) and start freeing from there, traversing back to the parent node.
The correct implementation here will be:free( memoryArea->newArea);
free(memoryArea); - Improper handling of return values At time, some functions return the reference to dynamically allocated memory. It becomes the responsibility of the
callingfunction to keep track of this memory location and handle it properly.char *func ( )
{
return malloc(20); // make sure to memset this location to ‘\0’…
}
void callingFunc ( )
{
func ( ); // Problem lies here
}
In the example above, the call to thefunc()function inside thecallingFunc()function is not handling the return address of the memory location. As a result, the 20 byte block allocated by thefunc()function is lost and results in a memory leak.




No comments:
Post a Comment