如何释放记忆? [英] how to release memory?

查看:54
本文介绍了如何释放记忆?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我使用free()函数释放内存,

但内存使用量仍在增加。


cygwin


#include< stdio.h>

#include< stdlib.h>

#include< string。 h>


struct queue {

char数据[1024];

struct queue * next;

};

struct queue * myqueue [1024];

struct queue * pre_item [1024];

struct queue * first_item [1024 ];


void enqueue(int sockfd,char * var){

myqueue [sockfd] =(struct queue *)malloc(sizeof(struct queue) );

strcpy((char *)& myqueue [sockfd] - > data,var);

myqueue [sockfd] - > next = NULL;

if(pre_item [sockfd]!= NULL){

pre_item [sockfd] - > next = myqueue [sockfd];

} else {

first_item [sockfd] = myqueue [sockfd];

}

pre_item [sockfd] = myqueue [sockfd];

}


char * dequeue(int sockfd){

c har r [1024];

struct queue * temp;

temp = first_item [sockfd];

strcpy(r,temp->数据);

first_item [sockfd] = first_item [sockfd] - >下一个;

temp = NULL;

free(temp);

返回r;

}


I use free() function to release memory,
But the memory usage is still increasing.

cygwin

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct queue{
char data[1024];
struct queue *next;
};
struct queue *myqueue[1024];
struct queue *pre_item[1024];
struct queue *first_item[1024];

void enqueue(int sockfd,char *var){
myqueue[sockfd]=(struct queue *) malloc(sizeof (struct queue));
strcpy((char *)&myqueue[sockfd]->data,var);
myqueue[sockfd]->next=NULL;
if (pre_item[sockfd]!=NULL){
pre_item[sockfd]->next=myqueue[sockfd];
}else{
first_item[sockfd]=myqueue[sockfd];
}
pre_item[sockfd]=myqueue[sockfd];
}

char *dequeue(int sockfd){
char r[1024];
struct queue *temp;
temp=first_item[sockfd];
strcpy(r,temp->data);
first_item[sockfd]=first_item[sockfd]->next;
temp=NULL;
free(temp);
return r;
}

推荐答案



" ; mynews" < no ****** @ gmail.comwrote in message

news:g3 ********* @ netnews.hinet.net ...

"mynews" <no******@gmail.comwrote in message
news:g3*********@netnews.hinet.net...

>

我使用free()函数释放内存,

但内存使用量仍在增加。
>
I use free() function to release memory,
But the memory usage is still increasing.



你做了什么来确定它?


请注意许多操作系统将保持

为应用程序预留的免费内存

直到它退出。


如何完成这取决于你的

操作系统,实现,以及可能的其他因素。


我没有密切关注你的代码,所以我不能

说它是否实际泄漏。


如果你想测试''真实''内存泄漏,

我建议为它制造的工具,例如valgrind。


-Mike

What did you do to determine that?

Note that many operating systems will keep
free''d memory in reserve for an application
until it exits.

How exactly this is done depends upon your
operating system, implementation, and possibly
other factors.

I didn''t look closely at your code, so I can''t
say if it actually leaks or not.

If you want to test for ''real'' memory leaks,
I suggest a tool made for it, e.g. valgrind.

-Mike


6月26日上午8:20,mynews < norep ... @ gmail.comwrote:
On Jun 26, 8:20 am, "mynews" <norep...@gmail.comwrote:

我使用free()函数释放内存,

但内存使用量是仍在增加。
I use free() function to release memory,
But the memory usage is still increasing.


temp = NULL;

free(temp);
temp=NULL;
free(temp);



从什么时候开始免费拨打电话(NULL)?

可能是你想要的:

free(temp);

temp = NULL;


我没有仔细看过你的代码,但方法似乎是

有缺陷。你正在维护3个指针数组,每个1024个元素,

来实现一个简单的队列。此外,你只释放

first_item中的元素(好吧,截至目前,甚至不是那个)。怎么样的元素

of per_item?你的逻辑对我来说并不清楚。也许你应该重新考虑你的数据结构和算法。

Since when are you allowed to call free(NULL)?
May be what you want is :
free(temp);
temp = NULL;

I have not seen your code carefully but the approach seems to be
flawed. You are maintaining 3 array of pointers, each 1024 elements,
to implement a simple queue. Further, you are freeing only elements in
first_item (well, as of now, not even that). What about the elements
of per_item? Your logic is not clear to me. Perhaps you should re-
consider your data structures and algorithms.


rahul说:
rahul said:

6月26日上午8:20,mynews < norep ... @ gmail.comwrote:
On Jun 26, 8:20 am, "mynews" <norep...@gmail.comwrote:

>我使用free()函数释放内存,
但内存使用量仍在增加。
>I use free() function to release memory,
But the memory usage is still increasing.


> temp = NULL;
free(temp);
> temp=NULL;
free(temp);



因为你什么时候可以免费拨打电话(NULL)?


Since when are you allowed to call free(NULL)?



至少从1989年开始。


它有点无意义,因为它什么都不做,但它''是合法的。

Since at least 1989.

It''s a bit pointless, since it does nothing whatsoever, but it''s legal.


可能是你想要的:

free(temp);

temp = NULL ;
May be what you want is :
free(temp);
temp = NULL;



可能,是的。


< snip>


-

Richard Heathfield< http://www.cpax.org.uk>

电子邮件:-http:// www。 + rjh @

谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>

Usenet是一个奇怪的放置" - dmr 1999年7月29日

Probably, yes.

<snip>

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999


这篇关于如何释放记忆?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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