这是一个用free()释放内存的正确方法 [英] IS this a proper way of freeing memory with free()

查看:150
本文介绍了这是一个用free()释放内存的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


这是一个小代码,


int main(无效)

{

char * p =(char *)malloc(100);

strcpy(p," Test1234567890");

p = p + 10;

免费(p);

/ ***这里是内存泄漏,因为p现在是

指向10位置过去分配内存的开始

**** /


/ **再加一些p的东西** /

< br $>
}

谢谢和问候,

Raman Chalotra

Hi All,

Here is a small Code,

int main(void)
{
char *p=(char *) malloc(100);
strcpy(p,"Test1234567890");
p=p+10;
free(p);
/*** Is here a memory Leak, because p is now
pointing 10 location past to the start of allocated memory
****/

/** some stuff with p again**/


}
Thanks and Regards,
Raman Chalotra

推荐答案

拉曼写道:
Raman wrote:

大家好,


这是一个小代码,


int main(无效)

{

char * p =(char *)malloc(100);
Hi All,

Here is a small Code,

int main(void)
{
char *p=(char *) malloc(100);



这里必须有多少次人们不得不说不要将返还的价值转换为
malloc的价值?有没有人在发布之前阅读档案?

How many times must people here have to say "do not cast the return
value of malloc"? Does anyone read the archive before they post?


strcpy(p," Test1234567890");

p = p + 10;

免费(p);

/ ***这里是内存泄漏,因为p现在是

指向10位置过去到分配的开始记忆

**** /
strcpy(p,"Test1234567890");
p=p+10;
free(p);
/*** Is here a memory Leak, because p is now
pointing 10 location past to the start of allocated memory
****/



这是完全未定义的行为。你的厕所可能会爆炸。不要

这样做。


-

Ian Collins。

It''s completely undefined behaviour. Your toilet might explode. Don''t
do it.

--
Ian Collins.


Raman写道:
Raman wrote:

char * p =(char *)malloc(100);
char *p=(char *) malloc(100);



char * p = malloc(100 * sizeof * p);


不要投出返回值malloc()。

char *p = malloc(100 * sizeof *p);

Don''t cast the return value from malloc().


strcpy(p," Test1234567890");

p = p + 10;

免费(p);

/ ***这里有一个内存泄漏,因为p现在是

指向10位置过去到分配内存的开始

**** /
strcpy(p,"Test1234567890");
p=p+10;
free(p);
/*** Is here a memory Leak, because p is now
pointing 10 location past to the start of allocated memory
****/



完全无效。 free()期望一个指针最初从

之一返回其他分配函数(calloc,malloc,realloc)。


NAME

免费分配内存


简介

#include< stdlib.h>


无空(void * ptr);


描述

free()函数将导致ptr指向的空间为

释放;也就是说,可供进一步分配。如果ptr是null

指针,则不会发生任何动作。否则,如果参数与之前由calloc(),malloc()或realloc()函数返回的

指针不匹配,或者

如果空间已经通过调用free()或realloc()解除分配,

行为未定义。


任何使用引用释放空间的指针都会导致undefined

行为。

Completely invalid. free() expects a pointer originally returned from one of
the other allocation functions (calloc, malloc, realloc).

NAME
free - free allocated memory

SYNOPSIS
#include <stdlib.h>

void free(void *ptr);

DESCRIPTION
The free() function shall cause the space pointed to by ptr to be
deallocated; that is, made available for further allocation. If ptr is a null
pointer, no action shall occur. 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.

Any use of a pointer that refers to freed space results in undefined
behavior.


Ian Collins写道:
Ian Collins wrote:

> char * p =(char *)malloc(100);
> char *p=(char *) malloc(100);



这里必须有多少次人们不得不说不要投出返还给malloc的
值?有没有人在发布之前阅读档案?


How many times must people here have to say "do not cast the return
value of malloc"? Does anyone read the archive before they post?



基本上永远。只要它仍然在人们阅读的书中 - 他们就会继续这样做。

Basically forever. As long as it''s still in books that people read - they''ll
keep doing it.


这篇关于这是一个用free()释放内存的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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