gpt4 book ai didi

c - 如何跟踪 malloc 和 free?

转载 作者:太空狗 更新时间:2023-10-29 15:14:17 24 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
Simple C implementation to track memory malloc/free?

我需要知道到目前为止我在 C 程序中使用了多少内存,这是伪代码

#include <stdio.h>

int usedMemory =0;

void *MyMalloc(int size){
usedMemory = usedMemory +size ;
return malloc(size);
}

void MyFree(void *pointer){
/*****************what should i write here????*************/
}
int main(int argc, char *argv[])
{
char *temp1= (char *)MyMalloc(100);
char *temp2= (char *)MyMalloc(100);

/*......other operations.........*/

MyFree(temp1);
MyFree(temp2);

return 0;
}

任何人都可以告诉我在 MyFree 方法中写什么(减少从 usedMemory 释放的内存量。

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com