malloc分配内存给指针 [英] malloc allocation of memory to pointers

查看:139
本文介绍了malloc分配内存给指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我按以下方式为结构指针分配内存

Hi,

I allocate memory for struct pointer as following manner

*p=(node*)malloc(2000);



我只使用很少的内存.我想找到未使用的内存形式,然后重复使用.

如果使用free(p),它将释放所有分配的内存,但是我只需要释放未使用的内存.

您能帮我吗?



I use little amount of memory only. I want to find unused memory form that and I want to reuse that.

if I use the free(p), it would free up all the allocated memory, but I need to free only the unused memory.

Can you help me?

推荐答案

为什么您不仅仅分配所需的内存?
您可以使用重新分配 [
Why don''t you allocate just the memory you need?
You may use realloc[^] if you need to resize the previously allocated memory.


*p=(node*)malloc(sizeof(node));


将分配node对象所需的确切内存量.


will allocate the exact amount of memory required for a node object.


这篇关于malloc分配内存给指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆