Wednesday, 3 July 2013

what is a memory leak | memory leak

when we allocate memory dynamically but somehow lose the way  to reach that memory then it is called as memory leak.

For example , suppose there is a function wherein we allocate dynamically and store its address in a pointer that is local to that function. when the control returns from the functioin , the local pointer dies losing the address of the memory it was pointing to. The dynamically allocated memory would continue to remain allocated , yet inaccessible . this allocated memory is leaked memory since we have lost the way to access it.
Example :-
           
           void function()
            {
                   int *p = new int;
                   // code;
            }

           void main()
          {
                function();      
          }      
        
       when control returns from the function memory gets leaked ;

 

0 comments:

Post a Comment