• 全部
...

用malloc申请了一块内存,可不可以分批释放

brookmill 2008-01-12 05:54:59
刚才突然冒出这么个奇怪的想法。
姑且不说这么做有没有什么实际意义,先讨论一下会不会引起内存泄漏或者指针越界之类的问题?
以下的代码我用VisualStudio的cl.exe编译运行,和预期的结果一样。
当然,这个程序太小了,说明不了什么问题。
  1. #include <stdio.h>
  2. #include <string.h>

  3. #define MAX 1000

  4. int main( void )
  5. {
  6. char *ptr;
  7. int length;

  8. ptr = (char *)malloc( MAX );
  9. if (ptr == NULL)
  10. exit (1);
  11. scanf( "%s", ptr );
  12. length = strlen( ptr );
  13. if (length < MAX - 1)
  14. free( ptr + length + 1);
  15. printf( "%s\n", ptr );
  16. free( ptr );
  17. return 0;
  18. }

或者这样:

  1. char *ptr;
  2. ptr = (char *)malloc( 1000 );
  3. if (ptr == NULL)
  4. exit (1);
  5. free( ptr + 500 );
  6. free( ptr + 200 );
  7. free( ptr );
...全文
给本帖投票
558 20 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
西山鸦 2008-01-14
  • 打赏
  • 举报
回复
不可以,系统分配内存时对每块内存都有记录,如果不顾及这些记录就硬性释放实际上没有释放,如果编译器能力强,会造成编译期错误,否则不报错,造成内存泄漏。
您的程序没有报错,属于编译器检查不严格。只有从头释放的操作才是有效的。
brookmill 2008-01-14
  • 打赏
  • 举报
回复
当时就是不知道为什么突然想到这个问题,不是为了实现什么功能,就当是没事活动活动脑子吧:)
谢谢各位的回答,又学了不少东西。
vecshid 2008-01-14
  • 打赏
  • 举报
回复
Mark,学习学习
p0303230 2008-01-14
  • 打赏
  • 举报
回复
你要清楚malloc怎么实现的才行
firemcu123 2008-01-14
  • 打赏
  • 举报
回复


11楼的说法不知道对不对?
  • 打赏
  • 举报
回复
显然这样释放会导致内存无法回收,但是有些编译器生成的东西不反馈异常给你。
你换成调试模式单步走一下试试
netxuning 2008-01-13
  • 打赏
  • 举报
回复
if (length < MAX - 1)
free( ptr + length + 1);
这样不行,在linux下会出现内存访问错误.
如果想节约空间的话可以这样:
if (length < MAX - 1)
realloc (ptr, length + 1);
CUG87525842 2008-01-13
  • 打赏
  • 举报
回复
看个各位大牛说的,长知识了!
ttlyfast 2008-01-13
  • 打赏
  • 举报
回复
用malloc申请了一块内存,可不可以分批释放

你要清楚malloc怎么实现的才行
lala_benben 2008-01-13
  • 打赏
  • 举报
回复
不知道楼主想实现什么功能...
你可以先申请一片总的内存,然后在这片内存上申请你需要的内存...使用完了归还给总内存,最后程序结束释放总内存
hastings 2008-01-12
  • 打赏
  • 举报
回复
想法是好,但你得找到支持你想法的证据,
在没找到前,不能轻信运行结果,因为C跟C++
不帮你检查内存漏了没有,越界了没有,等这些情况。
icoding 2008-01-12
  • 打赏
  • 举报
回复
楼主什么编译器编译的?刚拿你的代码在VC里编译就异常了,跟进去一看,异常发生在
free( ptr + length + 1);
可能就是我说的问题,会破坏堆的,总之问题多多
icoding 2008-01-12
  • 打赏
  • 举报
回复
嘿嘿,这个问题俺也来插两句,堆分配其实就是链表管理,每次申请内存产生一个节点,挂在busylist上,如果释放,那么就是把这个块从busylist上脱离,然后挂到freelist上,并且每个节点有头部记录上一节点位置,下一节点位置,以及本节点的大小


如按照你的做法,回收节点的一部分,那么好,下面有2个问题需要确定

1)每次被回收的内存也要被当作块,块就有头,记录了块大小等等消息,那么你这个块的块头是什么数据?不清楚,那么很可能就会破坏freelist

2)假如我再次使用malloc将你回收的内存分配出去了,但是你原先的块并不知道他的大小已经改变了啊...当整个快都被使用的时候,就有可能出现数据覆盖
justin_0009 2008-01-12
  • 打赏
  • 举报
回复
当然是可以的.但不是MALLOC.
jiajihe 2008-01-12
  • 打赏
  • 举报
回复
不可以!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
有空看看 c与指针
这里说的有!
tjltail 2008-01-12
  • 打赏
  • 举报
回复
应该是不可以的,下面的话从msdn上来的

The free function deallocates a memory block (memblock) that was previously allocated by a call to calloc, malloc, or realloc. The number of freed bytes is equivalent to the number of bytes requested when the block was allocated (or reallocated, in the case of realloc). If memblock is NULL, the pointer is ignored and free immediately returns. Attempting to free an invalid pointer (a pointer to a memory block that was not allocated by calloc, malloc, or realloc) may affect subsequent allocation requests and cause errors.

After a memory block has been freed, _heapmin minimizes the amount of free memory on the heap by coalescing the unused regions and releasing them back to the operating system. Freed memory that is not released to the operating system is restored to the free pool and is available for allocation again.

When the application is linked with a debug version of the C run-time libraries, free resolves to _free_dbg. For more information about how the heap is managed during the debugging process, see The CRT Debug Heap.
asmst 2008-01-12
  • 打赏
  • 举报
回复
我认为是可以的。
你可以用realloc()函数,逐步减小分配的内存,最后用free()释放。
Minkey 2008-01-12
  • 打赏
  • 举报
回复
这么做自然是不行的
因为malloc分配的内存除了满足用户索求的大小外,还另附了一个区域"cookie"(记录着一些控制信息,典型地是包含一个标志位标明内存可否被回收);free一次的话,这些控制信息即已"失效",不能在用于第二次了...
楼主可以看一看malloc和free的大体实现就知道了,例如这儿:
http://www.ibm.com/developerworks/cn/linux/l-memory/
BMCRNET 2008-01-12
  • 打赏
  • 举报
回复
帮顶
Oversense 2008-01-12
  • 打赏
  • 举报
回复
不行...

除非你自己写 编译器,不过写出来也不怎么"标准",可以当扩展

70,024

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
社区公告
暂无公告

试试用AI创作助手写篇文章吧

手机看
关注公众号

关注公众号

客服 返回
顶部