如何在本地函数中使用malloc-ed内存 [英] How can I use malloc-ed memory inside a local function

查看:105
本文介绍了如何在本地函数中使用malloc-ed内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的功能如下:


void rho(matrix_t * out,matrix_t * in)

{

static int firsttime = 1;

static matrix_t * words;

/ * ...其他变量... * /


if(firsttime){

firsttime = 0

words = malloc(NUM_WORDS * sizeof(matrix_t));

if(!单词){

/ *抱怨* /

}

/ *计算单词[0],...单词[NUM_WORDS-1] * /

}


/ *其他处理* /

}


这个想法是第一次运行函数运行时会计算出一些矩阵,然后可以用于后续运行。

然而,内存永远不会被释放。这是合法的,如果没有,

最好的方式是什么?


-

Simon Nickerson

I have a function which looks like this:

void rho(matrix_t *out, matrix_t *in)
{
static int firsttime = 1;
static matrix_t *words;
/* ... other variables ... */

if (firsttime) {
firsttime = 0
words = malloc(NUM_WORDS * sizeof(matrix_t));
if (!words) {
/* complain */
}
/* calculate words[0], ... words[NUM_WORDS-1] */
}

/* other processing */
}

The idea is that a number of matrices are computed the first time
the function is run, which can then be used for subsequent runs.
However, the memory never gets freed. Is this legal, and if not,
what''s the best way round it?

--
Simon Nickerson

推荐答案

sj***@cantab.net (Simon Nickerson)写道:
sj***@cantab.net (Simon Nickerson) writes:
然而,内存永远不会被释放。这是合法的,
However, the memory never gets freed. Is this legal,




C标准对此没有任何说明。是否它是好的

风格在过去一直没有达成共识的争论。


事实上,在某些环境中所有的记忆程序终止时自动释放
。在其他情况下,它不是。


Martin



The C standard doesn''t say anything about it. Whether or not it is good
style has already been debated here without a consensus in the past.

As matter of fact, in some environments all the memory is automatically
freed when a program terminates. In others, it''s not.

Martin


Simon Nickerson写道:
Simon Nickerson wrote:
我有一个看起来像这样的函数:
void rho(matrix_t * out,matrix_t * in)
{
static int firsttime = 1;
static matrix_t * words ;
/ * ......其他变量... * /

if(firsttime){
firsttime = 0


I希望实际的函数有一个分号。

words = malloc(NUM_WORDS * sizeof(matrix_t));


< snip>

这个想法是第一次运行函数时会计算出一些矩阵,然后就可以了用于后续运行。
然而,内存永远不会被释放。这是合法的,如果没有,
最好的方式是什么?
I have a function which looks like this: void rho(matrix_t *out, matrix_t *in)
{
static int firsttime = 1;
static matrix_t *words;
/* ... other variables ... */

if (firsttime) {
firsttime = 0
I hope the actual function has a semicolon there.
words = malloc(NUM_WORDS * sizeof(matrix_t));
<snip>
The idea is that a number of matrices are computed the first time
the function is run, which can then be used for subsequent runs.
However, the memory never gets freed. Is this legal, and if not,
what''s the best way round it?




这是完全合法的。它的好设计取决于你的程序是否需要保留这些矩阵直到它终止,

以及你的平台是否在终止后释放所有内容

(记住其中一个或两个都可以改变)。


如果你想释放它,我建议把这个功能放在一个

模块[1]本身,使矩阵对象成为局部全局[1],

并添加另一个释放它的函数。然后就打电话给

退出。


[1]我相信我的意思很明确,但是更有经验的

读者请告诉我你会称这些东西为什么?谢谢。


-

Tom Zych

这个电子邮件地址将在某个时候到期以阻止垃圾邮件发送者。

永久地址:echo''g******@cbobk.pbz''| rot13



It''s perfectly legal. Whether it''s good design depends on whether
your program will want to keep these matrices until it terminates,
and whether your platform frees everything upon termination
(bearing in mind that either or both could change).

If you want to free it, I''d suggest putting the function in a
module[1] by itself, make the matrix object a local global[1],
and add another function that frees it. Then just call that on
exit.

[1] I trust my meaning is clear, but would the more experienced
readers please tell me what you would call these things? Thanks.

--
Tom Zych
This email address will expire at some point to thwart spammers.
Permanent address: echo ''g******@cbobk.pbz'' | rot13


sj***@cantab.net (Simon Nickerson)写道:
sj***@cantab.net (Simon Nickerson) writes:
但是,内存永远不会被释放。这是合法的,
However, the memory never gets freed. Is this legal,




C标准对此没有任何说明。是否它是好的

风格在过去一直没有达成共识的争论。


事实上,在某些环境中所有的记忆程序终止时自动释放
。在其他情况下,它不是。


马丁



The C standard doesn''t say anything about it. Whether or not it is good
style has already been debated here without a consensus in the past.

As matter of fact, in some environments all the memory is automatically
freed when a program terminates. In others, it''s not.

Martin


这篇关于如何在本地函数中使用malloc-ed内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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