C免费 [英] C free

查看:55
本文介绍了C免费的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


有人可以澄清这个吗?


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

p = p + 50;

免费(p);


免费清除所有100字节的内存或只有50,因为现在是指向第51字节内存的


如何将这些malloc和免费链接在内部?


谢谢,

Reddy

解决方案



Reddy写道:

你好,

有人可以澄清这个吗?

char * p =(char *)malloc(100 * sizeof( char));
p = p + 50;
free(p);

是免费清除所有100字节的内存还是只有50字节
现在指向第51个字节的内存?


两者都不;它唤起了未定义的行为。您只能使用NULL指针或指针具有

值来调用free()

,该值之前由malloc或类似函数返回。

如何将这些malloc和免费链接在一起?




我不确定你在这里问的是什么,但我怀疑它已经结束了

实施。


Spiros Bousbouras


sp **** @ gmail.com 写道:

Reddy写道:

您好,

有人可以澄清一下吗?

char * p =(char *)malloc(100 * sizeof(char));
p = p + 50 ;
免费(p);

是否可以自由清除所有100个字节的内存或只有50个,因为它现在指向第51个字节的内存?



它唤起了未定义的行为。你只能用NULL指针或带有一个
值的指针来调用free()
,这个值之前是由malloc或类似的函数返回的。




此外,使用malloc还存在潜在问题。你没有b $ b不需要转换malloc的返回值(如果编译器抱怨

你还有别的错误)并且sizeof(char)的定义是1

它的使用毫无意义。所以malloc调用应该是


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





char * p = malloc(100);


在这一点上搜索小组进行大量讨论。

这些malloc是如何通过internelly自由链接的?



我不确定你在这里问什么,但我怀疑它是'实现。




我也怀疑它与任何正确编写的C程序无关。

-

Flash Gordon,生活在有趣的时代。

网站 - http://home.flash-gordon.me.uk/

comp.lang.c发布指南和介绍:
http://clc-wiki.net/wiki/Intro_to_clc




Re DDY" <他*************** @ gmail.com>写在

你好,

有人可以澄清这个吗?

char * p =(char *)malloc(100 * sizeof(char)) ;
p = p + 50;
免费(p);

是免费清除所有100个字节的内存还是只有50个,因为它指向51st现在的内存字节是什么?

这些malloc如何通过internelly自由链接?



malloc()的工作方式,至少在一个简单的系统上,是有一个很大的

内存池。

然后我们有这样的结构


struct blockcontrol

{

blockcontrol * prev;

blockcontrol * next;

size_t length;

};


当你调用malloc()时,它会找到一块足够大的连续内存块,然后它会填充blockcontrol结构,所以它知道分配的块有多大

,以及相邻分配的空闲或分配

块的位置。精确的细节各不相同,我们在这里不用担心。


现在是聪明的部分。它在填充的块控制结构之后立即返回指向内存的指针

。然后调用者可以根据自己的喜好使用

内存。

当调用者调用free时,free函数减去

blockcontrol的大小从指针结构,然后它可以访问所有

数据。它可以将内存返回到池中,合并连续的空闲块,

等等。


您将看到尝试释放一半的内存,通过在分配的块中间传递一个

指针,会破坏这个方案。

free()无法找到blockcontrol结构


Hello,

Can someone clarify this?

char *p = (char *) malloc (100*sizeof(char));
p=p+50;
free(p);

Does free clear all the 100 bytes of memory or only 50 as it is
pointing to 51st byte of memory now?

How are these malloc and free linked internelly?

Thanks,
Reddy

解决方案


Reddy wrote:

Hello,

Can someone clarify this?

char *p = (char *) malloc (100*sizeof(char));
p=p+50;
free(p);

Does free clear all the 100 bytes of memory or only 50 as it is
pointing to 51st byte of memory now?
Neither ; it evokes undefined behaviour. You can only call free()
with either the NULL pointer or with a pointer which has a
value which was previously returned by malloc or similar functions.
How are these malloc and free linked internelly?



I''m not sure what you''re asking here but I suspect it''s up to
implementation.

Spiros Bousbouras


sp****@gmail.com wrote:

Reddy wrote:

Hello,

Can someone clarify this?

char *p = (char *) malloc (100*sizeof(char));
p=p+50;
free(p);

Does free clear all the 100 bytes of memory or only 50 as it is
pointing to 51st byte of memory now?



Neither ; it evokes undefined behaviour. You can only call free()
with either the NULL pointer or with a pointer which has a
value which was previously returned by malloc or similar functions.



In addition there are potential problems with the use of malloc. You
don''t need to cast the return value of malloc (if the compiler complains
you have something else wrong) and sizeof(char) is 1 by definition so
its use is pointless. So the malloc call should be either

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

or

char *p = malloc(100);

Search the group for lots of discussion on this point.

How are these malloc and free linked internelly?



I''m not sure what you''re asking here but I suspect it''s up to
implementation.



I also suspect it is irrelevant to any properly written C program.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc



"Reddy" <he***************@gmail.com> wrote in

Hello,

Can someone clarify this?

char *p = (char *) malloc (100*sizeof(char));
p=p+50;
free(p);

Does free clear all the 100 bytes of memory or only 50 as it is
pointing to 51st byte of memory now?

How are these malloc and free linked internelly?


The way malloc() works, on a simple system at least, is that there is a big
pool of memory.
Then we have a structure something like this

struct blockcontrol
{
blockcontrol *prev;
blockcontrol *next;
size_t length;
};

When you call malloc(), it finds a block of contiguous memory that is large
enough, and then it fills in the blockcontrol structure, so it knows how big
the allocated block is, and where the adjacent allocated free or allocated
blocks are. The precise details vary and we won''t worry about them here.

Now comes the clever part. It returns a pointer to the memory immediately
after the block control structure it has filled out. The caller can then use
that memory as he likes.
When the caller calls free, the free function subtracts the size of a
blockcontrol structure from the pointer, and then it has access to all the
data. It can return the memory to the pool, merge consecutive free blocks,
and so on.

You will see that trying to free half the memory allocated, by passing a
pointer in the middle of the block allocated, will destroy this scheme.
free() won''t be able to find the blockcontrol structure


这篇关于C免费的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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