返回指针 [英] return of pointer

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

问题描述

您好,

如果我的函数在内存区域返回一个指针,我在哪里可以释放它?

ex:

char * foo()

{

char * p;

p = malloc(10);

strcpy(p, 某事);

返回(p);

}

无效栏()

{

char * p = foo();

printf("%s",p);

free(p);

}

但是强迫任何人在使用foo()之后调用free()不是优雅的解决方案,

对我来说...我怎么能避免posible内存泄漏,如果有人忘记了

免费电话()?


非常感谢!

-

Sergey Koveshnikov。

Hello,
If my function return a pointer on a memory area, where do I free it?
e.x.:
char *foo()
{
char *p;
p = malloc(10);
strcpy(p, "something");
return(p);
}
void bar()
{
char *p = foo();
printf("%s", p);
free(p);
}
But force anybody to calling free() after use foo() not elegant solution,
for my mind... How can I avoid posible memory leaks if somebody forget to
call free()?

Thanks a lot!
--
Sergey Koveshnikov.

推荐答案

文章< bo ********** @ dcs.eurocom.od.ua> ;,Sergey Koveshnikov写道:
In article <bo**********@dcs.eurocom.od.ua>, Sergey Koveshnikov wrote:
你好,
如果我的函数在内存区域返回一个指针,我在哪里可以释放它?
ex:
char * foo ()
{
char * p;
p = malloc(10);
strcpy(p,something);
return(p);
}
void bar()
{
char * p = foo();
printf("%s",p);
free(p);
}
但强迫任何人免费拨打电话( )使用后foo()不是优雅的解决方案,


也许没有,但只要你保持一致并记录

界面就可以了。

我的想法...如果有人忘记
免费拨打电话,我怎么能避免内存泄漏呢?
Hello,
If my function return a pointer on a memory area, where do I free it?
e.x.:
char *foo()
{
char *p;
p = malloc(10);
strcpy(p, "something");
return(p);
}
void bar()
{
char *p = foo();
printf("%s", p);
free(p);
}
But force anybody to calling free() after use foo() not elegant solution,
Maybe not, but as long as you are consistent and document the
interface it works well.
for my mind... How can I avoid posible memory leaks if somebody forget to
call free()?




你可以不要强迫别人记得打电话给免费()。


创建一个foo_alloc()和一个foo_free()函数可能会帮助

虽然(未经测试) :


#include< stdlib.h>

#include< stdio.h>


int foo_alloc(char ** p)

{

/ * * p的健全性检查* /


* p = malloc (10);

if(* p == NULL)返回1;

strcpy(* p," so预测);

返回0;

}


int foo_free(char ** p)

{

/ * * p的健全性检查* /


免费(* p);

* p = NULL;


返回0;

}


无效栏()

{

char * p;


foo_alloc(& p);

printf(" foo:%s \ n",p);


foo_free(& p);

}



-

Andreas K?h?ri



You can''t force someone to remember to call free().

Creating a foo_alloc() and a foo_free() function might help
though (untested):

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

int foo_alloc(char **p)
{
/* sanity checks of *p here */

*p = malloc(10);
if (*p == NULL) return 1;
strcpy(*p, "something");
return 0;
}

int foo_free(char **p)
{
/* sanity checks of *p here */

free(*p);
*p = NULL;

return 0;
}

void bar()
{
char *p;

foo_alloc(&p);
printf("foo: %s\n", p);

foo_free(&p);
}


--
Andreas K?h?ri


ie无法自动清理动态分配的内存,如果在任何地方都不需要
,除了''bar()'的上下文,不是吗?

-

Sergey Koveshnikov。
i.e. No way to do automatic cleanup dynamic allocated memory, if it''s
unnecessary anywhere, except context of ''bar()'', isn''t it?
--
Sergey Koveshnikov.


文章< bo ********** @ dcs.eurocom.od .ua> ;, Sergey Koveshnikov写道:
In article <bo**********@dcs.eurocom.od.ua>, Sergey Koveshnikov wrote:
即没有办法自动清理动态分配的内存,如果它在任何地方都没有必要,除了''bar()''的上下文,不是吗?
i.e. No way to do automatic cleanup dynamic allocated memory, if it''s
unnecessary anywhere, except context of ''bar()'', isn''t it?




C中没有内置垃圾收集器,没有。


-

Andreas K?h?ri



There is no built-in garbage collector in C, no.

--
Andreas K?h?ri


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

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