有些人可以告诉我:
int *p;
p=(int*)malloc(10*sizeof(int));
free(p);
或
int *p;
p=(int*)malloc(10*sizeof(int));
p=NULL;
有些人可以告诉我:
int *p;
p=(int*)malloc(10*sizeof(int));
free(p);
或
int *p;
p=(int*)malloc(10*sizeof(int));
p=NULL;
<代码>Free将处理下列记忆问题:p
各点——简单地将其分配给NUL
(因此,你将不出现记忆泄露)。
值得一提的是,把你的点名指定到<联合国free
,因为这将防止你意外地试图获得解放的记忆(这种记忆仍有可能,但绝对不能做到)。
在C区没有垃圾收集,因此,如果你没有明确释放记忆库,即使没有提及垃圾,也不会放开。 也许你们来自停车场收集语言的背景,因此可能难以改变你们的思维方式,但永远必须记住,以C等低水平语言“永久”释放所有资源。
Hope this helps Cheers
p is pointer (to a block allocated dynamically in mem或y ["on the Heap"])
This means that p is a variable which contains the address in mem或y of a particular block (或 some particular size, in the example a block big enough to hold 10 integers).
<代码>Free(p); 指示(Cruntime)的记忆管理逻辑,即以前由各页点可再利用的栏目所占用的记忆。
<代码>p = NUL; 规定p对NUL的价值(以前包含的地址已经丢失),但所标明的记忆栏目也仍在考虑使用。
可能有一些混淆,因为在 Java、C#、Adhur等人等语言中,仅仅将变数分配给民族解放军(或该问题的另一地址)将自动免除基本记忆(假设其他活变数中不存在其他提及这一地址的情况)。
C或C++的情况并非如此,造成以下错误:
free(p);
// possibly some some code etc.
// later:
p[6] = a ; // <<--- Ouch we re modifying whatever new variable is there !!!
或
// didn t call free(p)
p = NULL;
// now the mem或y allocated f或 p is held in mem或y even though it
// is not going to be used (assuming no copies of p 或 of pointers to part
// of that block were made.
The latter case is only wasteful of resources, the f或mer can lead to hard to find bugs.
因此,a norm C idiom 是:
free(p);
p = NULL;
如其他人所解释,不要求自由直接派任联合国利比里亚特派团,就会造成记忆泄露。 请参看link,以获得有关记忆泄露和其他记忆相关问题的明确细节。
更好的办法是首先释放记忆,然后将其交给民族解放军:
free(p);
p = NULL;
解放所分配的记忆可以将其定位,并允许在保存记忆点时在其他地方使用这种记忆。
给全国人民力量分配的记忆设置一个点,并不处理。
如果你采用一种使用高分辨率低的低分辨率,则对分配、使用和释放记忆的物品进行 de击,并且对分配给、使用和为全国扫盲运动提供记忆的物品采取同样的做法。
记忆管理取决于执行(见)。
1. 导言
int *p;
p= (int * ) malloc(10*sizeof(int));
free(p);
记忆被释放回头。 但是,点人仍然提到解放的记忆地点。 因此,如果进一步使用memory corruption。
因此,正确的做法是重新确定民族解放军的协调人,以避免进一步使用该协调人。
在C中不宜打上交点。
2. 结 论
int *p;
p=(int * )malloc(10*sizeof(int));
p=NULL;
这将产生memory 渗漏: 由于所分配的记忆在此没有放开。 你们刚刚重新成为民族解放军的协调人。
<代码>p=NUL不适用于记忆的定位。 如果你不需要使用<代码>p标明的记忆,那么,如果你再使用<代码>(<>free(<>>/code>,否则将会出现记忆泄露。 您还应在打电话<条码>免费(<>>>>/代码>后建立<条码>p=NUL,以避免今后再错误地查阅这一记忆。
For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...
最好、最小、最快、开放的来源、C/C++ 3d 提供方(在3ds max模型的支持下),而不是通用公平市价,
Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...
I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...
I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...
I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...
Is there anything other than DDD that will draw diagrams of my data structures like DDD does that runs on Linux? ddd is okay and runs, just kind of has an old klunky feeling to it, just wanted to ...
Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...