free()没用...... [英] free() is useless...

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

问题描述

嗯,它并没有用,它似乎对我不起作用。


基本上,当我从函数返回指针时,免费没有

似乎为我释放了记忆。以下是我想要做的事情的一个例子:


char * blah(void){

char * string ;


string =(char *)malloc(sizeof(" foobar")* sizeof(char));

strcpy(string," foobar" ;);


返回字符串;

}


int main(无效){

char * string;


string = blah();

printf("%s \ n",string);

免费(字符串);


返回0;

}

-----


我不太确定如何正确诊断这个...因为我还没有在相当长的一段时间里接触过美元。虽然,我现在遇到的问题很明显b / c该软件是一个高容量套接字服务器,我可以看看
看到`top`增加了流程大小。


任何帮助都会非常有用。

谢谢,

马特。

解决方案



" Matt Gostick" <毫安** @ crazylogic.net>在留言中写道

news:c4 ************************** @ posting.google.c om ...

好吧,它并没有用,它似乎对我不起作用。

基本上,当我从一个指针返回时功能,免费似乎不会为我释放记忆。


你怎么能说它没有释放内存。如果你在免费获得

内容之前

和''免费'之后发现它们是相同的那么,这是正常的 - 你的
$ b一旦你打电话给''免费'',$ b实现/操作系统可以用内存做任何事情。

以下是我试图尝试的一个例子执行:

首先包括标准标题..


#include< stdio.h>

#include< stdlib。 h取代; / *适用于malloc的正确原型* /

char * blah(void){
char * string;

string =(char *)malloc(sizeof( " foobar的")*的sizeof(char)的);


无需转换malloc的返回值。


string = malloc(sizeof(" foobar")* sizeof * string) ;

strcpy(string," foobar");

返回字符串;
}
int main(void){
char * string;


嘿,你为什么在这里定义''字符串'?

你在''blah''函数里面看到名字冲突吗?

将其作为参数传递。

string = blah();
printf("%s\ n",string);
免费(字符串);

返回0;
}

-----

我不太确定如何正确诊断这个...因为我在相当长的一段时间内没有触及过C.虽然,我现在遇到的问题很明显b / c该软件是一个高容量的套接字服务器,我可以看到过程大小随着`top`而增长。

任何帮助都会非常有用。
谢谢,
Matt。





" Ravi乌代" < RA ****** @ gmail.com>在消息中写道

news:1094106488.725114@sj-nntpcache-5 ...


" Matt Gostick" <毫安** @ crazylogic.net>在消息中写道
新闻:c4 ************************** @ posting.google.c om ...

好吧,它并没有用,它似乎对我来说似乎没有工作

基本上,当我从函数返回指针时,免费
似乎没有为我释放内存。
你怎么能说它没有释放内存。如果您



在免费之前提及内容
并且在''免费'之后并且发现它们是相同的那么,那就是
正常 - 您的实施/一旦你打电话给''免费'',操作系统就可以对内存做任何事情。


我认为他很明确的症状是什么

告诉他内存可能无法正常释放。
< blockquote class =post_quotes>以下是我尝试做的一个例子:
首先包括标准标题..

#include< stdio.h>
#include< stdlib.h> / *适用于malloc的正确原型* /

char * blah(void){
char * string;

string =(char *) malloc(sizeof(" foobar")* sizeof(char));



无需转换malloc的返回值。




除非你的编译器像我一样,否则会抛出以下错误

(不是警告):


编译JUNKER.C:

错误JUNKER.C 19:无法将''void *''转换为''char *''

函数main()


这就是为ANSI合规配置的时候

string = malloc(sizeof(" foobar")* sizeof * string);

strcpy (字符串,foobar);

返回字符串;
}
int main(void){
char * string;
嘿,你为什么在这里定义''字符串'?
你在''blah''函数里面看到名字冲突吗?



为什么?我敢打赌,世界上很多其他人都有相同的基本电话号码 - 有名称冲突吗?或者

像区号和国家代码这样的东西可以解决它们吗?

如何编写main()或任何其他函数的人告诉

内部名称在其他函数中是什么 - 一般来说?

Pass它作为一个参数而已。




在这种情况下,他仍然需要调用它 - 而且

名称字符串也一样好和其他任何一样 - 并通过

参考传递它。为什么在不需要时添加复杂性?


William L. Bahn写道:

" ; Ravi Uday" < RA ****** @ gmail.com>在消息中写道
新闻:1094106488.725114@sj-nntpcache-5 ...


无需转换返回值malloc。



除非你的编译器像我一样,会抛出以下错误
(不是警告):

编译JUNKER.C:<错误JUNKER.C 19:无法将''void *''转换为''char *''
函数main()

那就是它配置的时候符合ANSI标准




您的编译器已损坏,或者您正在调用ANSI C ++

合规性,而不是ANSI C合规性。


Well, it''s not useless, it just doesn''t seem to be working for me.

Basically, when I''m returning a pointer from a function, free does not
seem to be freeing the memory for me. Here is an example of what I am
trying to do:

char *blah (void) {
char *string;

string = (char *) malloc(sizeof("foobar")*sizeof(char));
strcpy(string, "foobar");

return string;
}

int main (void) {
char *string;

string = blah();
printf("%s\n", string);
free(string);

return 0;
}
-----

I''m not quite sure how to properly diagnose this... as I haven''t
touched C in quite some time. Although, the problem I am having now
is evident b/c the software is a high volume socket server and I can
see the process size growing with `top`.

Any help would be very much appretiated.
Thanks,
Matt.

解决方案


"Matt Gostick" <ma**@crazylogic.net> wrote in message
news:c4**************************@posting.google.c om...

Well, it''s not useless, it just doesn''t seem to be working for me.

Basically, when I''m returning a pointer from a function, free does not
seem to be freeing the memory for me.
How can you say that it is not freeing the memory. If you are referring to
contents before free
and after ''free'' and finding it to be same then, that is normal - your
implementation/OS can do
anything with the memory once you call ''free''.
Here is an example of what I am
trying to do:
Include standard headers first..

#include <stdio.h>
#include <stdlib.h> /* For proper prototype of malloc*/
char *blah (void) {
char *string;

string = (char *) malloc(sizeof("foobar")*sizeof(char));
No need to cast the return value of malloc.

string = malloc(sizeof("foobar")*sizeof *string );
strcpy(string, "foobar");

return string;
}

int main (void) {
char *string;
Hey why are you defining ''string'' here ?
Dont you see a name conflict when inside ''blah'' function ?
Pass it as a parameter instead.

string = blah();
printf("%s\n", string);
free(string);

return 0;
}
-----

I''m not quite sure how to properly diagnose this... as I haven''t
touched C in quite some time. Although, the problem I am having now
is evident b/c the software is a high volume socket server and I can
see the process size growing with `top`.

Any help would be very much appretiated.
Thanks,
Matt.




"Ravi Uday" <ra******@gmail.com> wrote in message
news:1094106488.725114@sj-nntpcache-5...


"Matt Gostick" <ma**@crazylogic.net> wrote in message
news:c4**************************@posting.google.c om...

Well, it''s not useless, it just doesn''t seem to be working for me.
Basically, when I''m returning a pointer from a function, free does not seem to be freeing the memory for me.
How can you say that it is not freeing the memory. If you are


referring to contents before free
and after ''free'' and finding it to be same then, that is normal - your implementation/OS can do
anything with the memory once you call ''free''.
I think he was pretty explicit on what the symptoms are that are
telling him that the memory may not be getting freed properly.

Here is an example of what I am
trying to do:
Include standard headers first..

#include <stdio.h>
#include <stdlib.h> /* For proper prototype of malloc*/

char *blah (void) {
char *string;

string = (char *) malloc(sizeof("foobar")*sizeof(char));



No need to cast the return value of malloc.



Unless your compiler, like mine, would throw the following error
(not warning):

Compiling JUNKER.C:
Error JUNKER.C 19: Cannot convert ''void *'' to ''char *'' in
function main()

And that''s when it is configured for ANSI compliance

string = malloc(sizeof("foobar")*sizeof *string );

strcpy(string, "foobar");

return string;
}

int main (void) {
char *string;
Hey why are you defining ''string'' here ?
Dont you see a name conflict when inside ''blah'' function ?



Why? I''ll bet a lot of other people in the world have the same
basic phone number you do - is there a name conflict? Or do
things like area codes and country codes resolve them? How does
the person writing main() or any other function tell what the
internal names are within other functions - in general?
Pass it as a parameter instead.



In which case he would still have to call it something - and the
name string is just as good as any other - and pass it by
reference. Why add the complexity when it''s not needed?



William L. Bahn wrote:

"Ravi Uday" <ra******@gmail.com> wrote in message
news:1094106488.725114@sj-nntpcache-5...


No need to cast the return value of malloc.


Unless your compiler, like mine, would throw the following error
(not warning):

Compiling JUNKER.C:
Error JUNKER.C 19: Cannot convert ''void *'' to ''char *'' in
function main()

And that''s when it is configured for ANSI compliance



Your compiler is broken or else you are invoking it for ANSI C++
compliance, not ANSI C compliance.


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

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