title: tracking dynamic memory allocation in c/c++
body: <p>In c or c++, Is there any way to keep track of dynamically allocated memory. Say i have code like this</p>
  
  <pre><code>void somefunction(some arguments,long mc){
  //allocate b bytes of memory using malloc,calloc or new
  mc += b;
  
  
  //allocate once again, say p bytes
  mc += p;
  
  
  //deallocate q bytes using delete or free()
  mc -= q;
  
  print "mc bytes allocated at this point";
  
  } 
  </code></pre>
  
  <p>one could declare mc as global and use it in all functions. The problem is when memory is deallocated, there is no way of knowing how much memory was just deallocated so how does one update mc in this case.</p>