free函数的问题?

qqq123 2009-03-17 05:08:18
代码如下:
int main()
{
void * address = malloc(32);
free(address + 1);
return 0;
}
请问free函数可以释放掉malloc所分配的内存吗?
...全文
139 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
Rain208 2009-03-20
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 baihacker 的回复:]
你所请求的回收的地址,不是malloc得到的...有着未定义的行为
©ISO/IEC ISO/IEC 9899:1999 (E)
Description
2 The free function causes the space pointed to by ptr to be deallocated, that is, made
available for further allocation. If ptr is a null pointer, no action occurs. Otherwise, if
the argument does not match a pointer earlier returned by the calloc, malloc, or
realloc function, or if t…
[/Quote]
up
只要是因为你穿过去的地址不是由malloc 等申请的
也就是不合法的参数
看编译器的处理了
qqq123 2009-03-20
  • 打赏
  • 举报
回复
如果将malloc改成vmalloc不是结果如何?
lwouyang 2009-03-18
  • 打赏
  • 举报
回复
不可能。为什么要这么做呢?
cutrain2009 2009-03-18
  • 打赏
  • 举报
回复
内存访问会越界的
就是会多释放该动态空间的后一个单元
会出错的
mkuymkuy 2009-03-17
  • 打赏
  • 举报
回复
关键是VOID指针的偏移量不知道,如果让指针加一,不知道编译器怎么处理,我估计不同的编译器的处理方法不同...
Darkneece 2009-03-17
  • 打赏
  • 举报
回复

int main()
{
char* address = malloc(32);
*(unsigned long *)address = 123;
*(unsigned long *)(address + 4) = 456;
printf("%d ",*(unsigned long *)address);
printf("%d ",*(unsigned long *)(address + 4));
free(address + 4);
printf("%d ",*(unsigned long *)address);
printf("%d ",*(unsigned long *)(address + 4));
return 0;
}

在VC的debug模式下会报错,但那是VCdebugger报的断言错。
用release模式运行没有问题

不过这种方法似乎不能测试内存是否正确释放了

int main()
{
char * address = malloc(32);
*(unsigned long *)address = 123;
*(unsigned long *)(address + 4) = 456;
printf("%d ",*(unsigned long *)address);
printf("%d ",*(unsigned long *)(address + 4));
//free(address + 4);
free(address);
*(unsigned long *)address = 789;
*(unsigned long *)(address + 4) = 999;
printf("%d ",*(unsigned long *)address);
printf("%d ",*(unsigned long *)(address + 4));
return 0;
}

这回不管是debug和release都没问题,应该是这块内存还没有分给其他进程

所以应该如飞雪所说:结果不可预知
breezes2008 2009-03-17
  • 打赏
  • 举报
回复
不能。
lingyin55 2009-03-17
  • 打赏
  • 举报
回复
up
qqq123 2009-03-17
  • 打赏
  • 举报
回复
我用MinGW(GCC)到没有crash,但是的确不能释放内存,代码如下:
int main()
{
void * address = malloc(32);
*(unsigned long *)address = 123;
*(unsigned long *)(address + 4) = 456;
printf("%d ",*(unsigned long *)address);
printf("%d ",*(unsigned long *)(address + 4));
free(address + 4);
printf("%d ",*(unsigned long *)address);
printf("%d ",*(unsigned long *)(address + 4));
return 0;
}
运行结果:
123 456 123 456
把free(address + 4);换成free(address);运行结果:
123 456 0 456
0可能是下一个空白堆块的索引。


flameearth 2009-03-17
  • 打赏
  • 举报
回复
不能 你加一后 就不是它志向的首地址了
arong1234 2009-03-17
  • 打赏
  • 举报
回复
那不是关键,呵呵,楼主肯定是想问能不能free调整后的指针
[Quote=引用 6 楼 Dinelgua 的回复:]

Vc6编译时会报下面错误
error C2036: “void *” : 未知的大小


因为内存申请时,系统会记录此次申请内存的大小放到一个内存头中
这个内存头需要address地址偏移后得到,而address+1后偏移是不能得到正确的内存大小的
所以不能这么写
[/Quote]
aozhi 2009-03-17
  • 打赏
  • 举报
回复
mark
Dinelgua 2009-03-17
  • 打赏
  • 举报
回复

Vc6编译时会报下面错误
error C2036: “void *” : 未知的大小


因为内存申请时,系统会记录此次申请内存的大小放到一个内存头中
这个内存头需要address地址偏移后得到,而address+1后偏移是不能得到正确的内存大小的
所以不能这么写
baihacker 2009-03-17
  • 打赏
  • 举报
回复
你所请求的回收的地址,不是malloc得到的...有着未定义的行为
©ISO/IEC ISO/IEC 9899:1999 (E)
Description
2 The free function causes the space pointed to by ptr to be deallocated, that is, made
available for further allocation. If ptr is a null pointer, no action occurs. Otherwise, if
the argument does not match a pointer earlier returned by the calloc, malloc, or
realloc function, or if the space has been deallocated by a call to free or realloc,
the behavior is undefined.
arong1234 2009-03-17
  • 打赏
  • 举报
回复
不能,这会导致程序崩溃,free释放的地址必须严格等于malloc返回的地址
Clerk_9919 2009-03-17
  • 打赏
  • 举报
回复
会直接导致程序Crash
yangch_nhcmo 2009-03-17
  • 打赏
  • 举报
回复

int main()
{
void * address = malloc(32);
free(address); //不要加1
return 0;
}

Clerk_9919 2009-03-17
  • 打赏
  • 举报
回复
不能

69,336

社区成员

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

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