在哪里分配内存 [英] where to allocate memory

查看:59
本文介绍了在哪里分配内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个函数,给出nx1向量a和nx1 b求解n * 1 c的方程f(a,c)= b的系统。

编写函数时:

1]我应该在函数内为c分配内存并返回

分配的内存吗?

导致

double * solve(const double * a,const double * b,int n)

{

double * c;

/ * blah blah * /

c = malloc(n * sizeof(* c));

/ * blah blah * /

返回c;

}


或者,


2]我应该在函数外部分配内存,然后传递它

作为参数?


void solve(const double * a,const double * b,double * c ,int n);

/ **** /

c = malloc(n * sizeof(* c));

solve(a ,b,c,n);

解决方案

" gc" <顾********** @ yahoo.com>写道:

在编写函数时:
1]我应该在函数内为c分配内存并返回分配的内存吗?
或者,
> 2]我应该在函数外部分配内存然后将其作为参数传递吗?




两者都是C中的有效解决方案。我通常更喜欢后一种形式

因为它让来电者可以更好地控制他们想要如何管理内存。 (也许使用自动数组而不是malloced

存储)。我认为将malloc及其

在相同功能中免费对应也看起来更清晰。


double a = malloc(n * sizeof * c) ;

double b = malloc(n * sizeof * c);


double c = malloc(n * sizeof * c);

if(a&& b& c)和/ b
*

/ *设置a,b * /

求解(a,b,c,n);

/ *使用c * /

免费(a);

免费(b) );

免费(c);

}


或:


double a [] = {1,2 / * ... * /};

double b [] = {1,2 / * ... * /};

double c [sizeof a / sizeof * a];

求解(a,b,c,sizeof a / sizeof * a);

/ *使用c * /


-

Simon。


gc写道:

我正在编写一个函数,给出nx1向量a和nx1 b求解方程组f(a,c)= b表示nx1 c。

写这个功能:
1]我应该分配在函数内吃了c的内存并返回分配的内存?


如果你愿意的话。

导致
double * solve(const double * a,const double * b,int n)
{
double * c;
/ * blah blah * /
c = malloc(n * sizeof(* c));


if(c!= NULL)

{

/ * blah blah * /


}

返回c;
}

或者,

2]我应该在函数外部分配内存吗?然后传递它作为参数?


如果你愿意的话。

void solve(const double * a,const double * b,double * c,int n);

/ **** /
c = malloc(n * sizeof(* c));
求解(a,b,c,n);




这是一个只有你能做出的设计决定。但是我会以这种方式来看待这种方式 - 如果你需要很多解决方案来解决不同的问题

,那么可能你就是''我会发现分配内存更方便

功能。另一方面,如果您的典型用法是/ b $ b场景是你解决/一个/问题,那么另一个,然后是另一个,

没有需要共存的解决方案那么在内存中传递

可能更方便。这样,如果您愿意,可以重复使用它。或者,在指向内存的指针中传递

,并获得solve()以适当调整其大小。


-

Richard Heathfield: bi****@eton.powernet.co.uk

Usenet是一个奇怪的地方。 - Dennis M Ritchie,1999年7月29日。

C FAQ: http://www.eskimo.com/~scs/C-faq/top.html

K& R答案,C书等:< a rel =nofollowhref =http://users.powernet.co.uk/etontarget =_ blank> http://users.powernet.co.uk/eton


如果没有充分的理由在函数中分配它,那么我更喜欢

让调用者分配。你可能总是记得自由,但其他人可能不会。如果你让他们写malloc,他们不会忘记。


此外,调用者分配从代码重用点稍微好一点

view 。你的解算器现在与malloc绑定,而有些人使用不同的内存管理方案。我不知道你在使用什么系统,但是如果你使用多个堆或类似的东西,你想要让调用者分配




但是如果你只是在写一些小巧,快速的应用程序,那么可能没有

的差异。


gc <顾********** @ yahoo.com>在消息中写道

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

我正在编写一个函数,给出nx1向量a和nx1 b求解方程组f(a,c)= b表示nx1 c。
1]我应该在函数内为c分配内存并返回分配的内存吗?
导致
double * solve(const)的内容double * a,const double * b,int n)
{
double * c;
/ * blah blah * /
c = malloc(n * sizeof(* c) );
/ * blah blah * /
返回c;
}

或者,

2]我应该在外面分配内存吗?函数然后传递它作为参数?

void solve(const double * a,const double * b,double * c,int n);

/ **** /
c = malloc(n * sizeof(* c));
求解(a,b,c,n);



I am writing a function that given nx1 vector a and a nx1 b solves a
system of equations f(a,c)=b for a nx1 c.

While writing the function:
1] Should I allocate the memory for c within the function and return
the allocated memory?
something that leads to
double *solve(const double *a,const double *b,int n)
{
double *c;
/*blah blah*/
c=malloc(n*sizeof(*c));
/*blah blah*/
return c;
}

or,

2] Should I allocate the memory outside the function and then pass it
as a parameter?

void solve(const double *a,const double *b,double *c,int n);
/****/
c=malloc(n*sizeof(*c));
solve(a,b,c,n);

解决方案

"gc" <gu**********@yahoo.com> wrote:

While writing the function:
1] Should I allocate the memory for c within the function and
return the allocated memory?
or,
2] Should I allocate the memory outside the function and then
pass it as a parameter?



Both are valid solutions in C. I generally prefer the latter form
as it gives the caller more control over how they want to manage
the memory. (Perhaps using an automatic array rather than malloced
storage). I think it also looks cleaner to have the malloc and its
corresponding free in the same function.

double a = malloc(n * sizeof *c);
double b = malloc(n * sizeof *c);

double c = malloc(n * sizeof *c);
if(a && b && c)
{
/* set up a, b */
solve(a, b, c, n);
/* use c */
free(a);
free(b);
free(c);
}

Or:

double a[] = {1, 2 /* ... */};
double b[] = {1, 2 /* ... */};
double c[sizeof a / sizeof *a];
solve(a, b, c, sizeof a / sizeof *a);
/* use c */

--
Simon.


gc wrote:

I am writing a function that given nx1 vector a and a nx1 b solves a
system of equations f(a,c)=b for a nx1 c.

While writing the function:
1] Should I allocate the memory for c within the function and return
the allocated memory?
If you like.
something that leads to
double *solve(const double *a,const double *b,int n)
{
double *c;
/*blah blah*/
c=malloc(n*sizeof(*c));
if(c != NULL)
{
/*blah blah*/
}
return c;
}

or,

2] Should I allocate the memory outside the function and then pass it
as a parameter?
If you like.

void solve(const double *a,const double *b,double *c,int n);
/****/
c=malloc(n*sizeof(*c));
solve(a,b,c,n);



It''s a design decision that only you can make. But I would look at it this
way - if you''re going to need many solutions to different problems to exist
at the same time, then probably you''ll find it more convenient for the
function to allocate the memory. If, on the other hand, your typical usage
scenario is that you solve /one/ problem, then another, then another,
without the solutions needing to co-exist, then it may be handier to pass
in the memory. That way, you can re-use it if you wish. Alternatively, pass
in a pointer to the memory, and get solve() to resize it as appropriate.

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton


If there''s no good reason to allocate it in the function, then I prefer to
have the caller allocate. You might always remember to free, but others
might not. If you make them write the malloc, they won''t forget.

Also, the caller allocating is slightly better from a code reuse point of
view. Your solver is now tied to malloc, whereas some people use different
memory management schemes. I have no idea what system you''re on, but if you
use multiple heaps or anything like that, you want to have the caller
allocate.

But if you''re just writing some small, quick app, there''s probably no
difference.

"gc" <gu**********@yahoo.com> wrote in message
news:79**************************@posting.google.c om...

I am writing a function that given nx1 vector a and a nx1 b solves a
system of equations f(a,c)=b for a nx1 c.

While writing the function:
1] Should I allocate the memory for c within the function and return
the allocated memory?
something that leads to
double *solve(const double *a,const double *b,int n)
{
double *c;
/*blah blah*/
c=malloc(n*sizeof(*c));
/*blah blah*/
return c;
}

or,

2] Should I allocate the memory outside the function and then pass it
as a parameter?

void solve(const double *a,const double *b,double *c,int n);
/****/
c=malloc(n*sizeof(*c));
solve(a,b,c,n);



这篇关于在哪里分配内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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