free()的结尾在哪里? [英] where is the end of free()?

查看:92
本文介绍了free()的结尾在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人会告诉我:

s1:int * ptr =(int *)malloc(sizeof(int));


s2:int * ptr =(int *)malloc(n * sizeof(int));


当我使用免费(ptr),


什么是两个陈述之间的区别?

提前感谢。

would someone tell me:
s1: int *ptr = (int *) malloc (sizeof(int));

s2: int *ptr = (int *) malloc (n * sizeof(int));

when i use free(ptr),

what is the difference between the two statements?
thanks in advance.

推荐答案

Thomas Zhu写道:
Thomas Zhu wrote:
会有人告诉我:

s1:int * ptr =(int *)malloc(sizeof(int));

s2:int * ptr =(int *)malloc(n * sizeof(int));
would someone tell me:
s1: int *ptr = (int *) malloc (sizeof(int));

s2: int *ptr = (int *) malloc (n * sizeof(int));




请删除演员表。你不需要施放

malloc返回的值。请包含stdlib.h。


以上两个语句的区别在于可以重复使用的字节数





Please remove the cast. you don''t need to cast the value returned by
malloc. Please include stdlib.h.

The difference in the above two statements is the number of bytes
which are marked as reusable.


谢谢。


但是:


ptr =(int *)malloc(n * sizeof(int));

ptr ++;


free(ptr);


系统免费的存储单元或n-1个存储单元?


以及为什么不需要演员?

thanks.

but :

ptr = (int *) malloc (n * sizeof(int));
ptr ++;

free(ptr);

does the system free n mem-units or n-1 mem-units?

and why the cast is not necessary?


Le 21-10-2005,Thomas Zhu< zh ******** @ gmail.com> aécrit*:
Le 21-10-2005, Thomas Zhu <zh********@gmail.com> a écrit*:
ptr =(int *)malloc(n * sizeof(int));
ptr ++;

free(ptr);

系统是否有n个存储单元或n-1个存储单元?


两者都不是。这是UB。

以及为什么不需要演员?
ptr = (int *) malloc (n * sizeof(int));
ptr ++;

free(ptr);

does the system free n mem-units or n-1 mem-units?
Neither one nor the other. This is UB.
and why the cast is not necessary?




因为malloc返回一个void *指针,它就是

可以隐含地转换为int *。


Marc Boyer



Because malloc returns a void* pointer, and it
can be implicitely converted into int*.

Marc Boyer


这篇关于free()的结尾在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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