在 c 或 c++ 中,有没有办法跟踪动态分配的内存。说我有这样的代码
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";
}
可以将 mc 声明为全局并在所有函数中使用它。问题是当内存被释放时,没有办法知道刚刚释放了多少内存,所以在这种情况下如何更新 mc 。