将堆分配给不可用的函数静态变量。 [英] Allocating heap to unfreeable function static variables.

查看:60
本文介绍了将堆分配给不可用的函数静态变量。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想出了这样一个习惯用法,其中一个函数需要一个变量

的内存量为它的临时存储空间,以避免不断地使用
mallocing。这让我感到有点不舒服,因为有不可思议的b / b
(但仍然是技术上可到达的)内存,但它确实是,我认为仍然是一件有用的事情:


void fn(int * stuff,int nstuffs)

{

static int * temp_stuff = NULL;

static int ntemp_stuffs = 0;


if(nstuffs ntemp_stuffs){

ntemp_stuffs = nstuffs;

temp_stuff = realloc( temp_stuff,ntemp_stuffs * sizeof(int));

}


/ *使用临时东西* /

memcpy (temp_stuff,stuff,nstuffs * sizeof(int));

}


这是一件可怕的事吗?


干杯,

查理

I came up with this idiom for cases where a function needs a variable
amount of memory for it''s temporary storage so as to avoid constantly
mallocing. It makes me feel a little uncomfortable to have unfreeable
(but still technically reachable) memory hanging around but it is, I
think still a useful thing to do:

void fn(int *stuff, int nstuffs)
{
static int *temp_stuff = NULL;
static int ntemp_stuffs = 0;

if(nstuffs ntemp_stuffs) {
ntemp_stuffs = nstuffs;
temp_stuff = realloc(temp_stuff, ntemp_stuffs * sizeof(int));
}

/* Use the temp stuffs for something */
memcpy(temp_stuff, stuff, nstuffs * sizeof(int));
}

Is this a horrible thing to do?

Cheers,
Charlie

推荐答案

查理< ch ********** ***@gmail.com写的:
charlie <ch*************@gmail.comwrites:

我想出了一个函数需要变量的情况

内存量这是临时性的年龄,以避免不断
mallocing。这让我感到有点不舒服,因为有不可思议的b / b
(但仍然是技术上可到达的)内存,但它确实是,我认为仍然是一件有用的事情:


void fn(int * stuff,int nstuffs)

{

static int * temp_stuff = NULL;

static int ntemp_stuffs = 0;


if(nstuffs ntemp_stuffs){

ntemp_stuffs = nstuffs;

temp_stuff = realloc( temp_stuff,ntemp_stuffs * sizeof(int));

}


/ *使用临时东西* /

memcpy (temp_stuff,stuff,nstuffs * sizeof(int));

}


这是一件可怕的事吗?
I came up with this idiom for cases where a function needs a variable
amount of memory for it''s temporary storage so as to avoid constantly
mallocing. It makes me feel a little uncomfortable to have unfreeable
(but still technically reachable) memory hanging around but it is, I
think still a useful thing to do:

void fn(int *stuff, int nstuffs)
{
static int *temp_stuff = NULL;
static int ntemp_stuffs = 0;

if(nstuffs ntemp_stuffs) {
ntemp_stuffs = nstuffs;
temp_stuff = realloc(temp_stuff, ntemp_stuffs * sizeof(int));
}

/* Use the temp stuffs for something */
memcpy(temp_stuff, stuff, nstuffs * sizeof(int));
}

Is this a horrible thing to do?



将静态变量命名为temp肯定是可怕的什么。


但是要回答你的问题,这简直太糟糕了。你没有概念

的所有权,这可能会导致以后可怕的错误

其他人调用fn()的行没有注意到

已经拥有它假装要守的内容。

It is certainly horrible to name static variables "temp" anything.

But to answer your question this is simply horrible. You have no concept
of ownership and this could and will lead to horrific bugs later down
the line where someone else calls fn() without paying proper heed to
already "owns" the contents it pretends to guard.


9月12日上午10点58分,查理< ch ************* @ gmail.comwrote:
On Sep 12, 10:58*am, charlie <ch*************@gmail.comwrote:

我想出了一个函数需要一个变量

内存量的情况下这个成语临时存放,以免不断地使用
mallocing。这让我感到有点不舒服,因为有不可思议的b / b
(但仍然是技术上可到达的)内存,但它确实是,我认为仍然是一件有用的事情:


void fn(int * stuff,int nstuffs)

{

* * * static int * temp_stuff = NULL;

* * * static int ntemp_stuffs = 0;


* * * if(nstuffs ntemp_stuffs){

* * * * * ntemp_stuffs = nstuffs;

* * * * * temp_stuff = realloc(temp_stuff,ntemp_stuffs * sizeof(int));

* * *}


* * * / *使用临时东西* /

* * * memcpy(temp_stuff,stuff,nstuffs * sizeof(int));


}


这是一件很可怕的事情吗?
I came up with this idiom for cases where a function needs a variable
amount of memory for it''s temporary storage so as to avoid constantly
mallocing. It makes me feel a little uncomfortable to have unfreeable
(but still technically reachable) memory hanging around but it is, I
think still a useful thing to do:

void fn(int *stuff, int nstuffs)
{
* * *static int *temp_stuff = NULL;
* * *static int ntemp_stuffs = 0;

* * *if(nstuffs ntemp_stuffs) {
* * * * *ntemp_stuffs = nstuffs;
* * * * *temp_stuff = realloc(temp_stuff, ntemp_stuffs * sizeof(int));
* * *}

* * */* Use the temp stuffs for something */
* * *memcpy(temp_stuff, stuff, nstuffs * sizeof(int));

}

Is this a horrible thing to do?



这是相当不方便和不直观的,不是提到它不是
线程安全。另外,你没有检查realloc()是否成功,并且如果你做检查则确定
,如果它失败了,你只能在没有做任何事情的情况下返回

因为你的函数返回void。解决方案是使用

VLA(可变长度阵列)。


Sebastian

It''s rather inconvenient and unintuitive, not to mention it isn''t
thread-safe. Also, you didn''t check whether realloc() succeeded, and
if you do check, and if it fails, you can only return without doing
anything because your function returns void. The solution is to use a
VLA (variable-length array).

Sebastian

charlie写道:
charlie wrote:

我想出了一个函数需要变量的情况

内存量'的临时存储,以避免不断
mallocing。这让我感到有点不舒服,因为有不可思议的b / b
(但仍然是技术上可到达的)内存,但它确实是,我认为仍然是一件有用的事情:


void fn(int * stuff,int nstuffs)

{

static int * temp_stuff = NULL;

static int ntemp_stuffs = 0;


if(nstuffs ntemp_stuffs){

ntemp_stuffs = nstuffs;

temp_stuff = realloc( temp_stuff,ntemp_stuffs * sizeof(int));

}


/ *使用临时东西* /

memcpy (temp_stuff,stuff,nstuffs * sizeof(int));

}


这是一件可怕的事吗?


干杯,

查理
I came up with this idiom for cases where a function needs a variable
amount of memory for it''s temporary storage so as to avoid constantly
mallocing. It makes me feel a little uncomfortable to have unfreeable
(but still technically reachable) memory hanging around but it is, I
think still a useful thing to do:

void fn(int *stuff, int nstuffs)
{
static int *temp_stuff = NULL;
static int ntemp_stuffs = 0;

if(nstuffs ntemp_stuffs) {
ntemp_stuffs = nstuffs;
temp_stuff = realloc(temp_stuff, ntemp_stuffs * sizeof(int));
}

/* Use the temp stuffs for something */
memcpy(temp_stuff, stuff, nstuffs * sizeof(int));
}

Is this a horrible thing to do?

Cheers,
Charlie



这取决于你使用它时的纪律。


我在我的IDE编程中遇到了类似的问题
当我在Windows 3.1(16位)下编程时



要避免每次我需要一些缓冲区来进行mallocing

并节省最初的堆栈空间

我在启动时分配全局buf 35K


我把它用作临时字符串之前我把它们填充到列表框中,以便在函数之间传递数据


使用。


然后,Windows 95出现了,内存和堆栈没有

这很重要,尽管仍然很重要。 IDE gew,

我添加了一个调试器,并且buf开始产生奇怪的

问题。一个程序会使用buf将数据传递给另一个

,但该调用树中的一些例程也会使用buf


这会产生很多错误,并导致我要重新改写

从软件中消除buf(尽我所能)


规则是:


绝不假设buf通过函数调用活着

仅在单线程中使用buf,也许是主线程。

这是一个可怕的事情要做什么?


在Vista 64下是的。

在windows 3.1下没有

在一个小的嵌入式系统下?也许,这取决于系统的安全性

要求。

-

jacob navia

jacob at jacob point remcomp point fr

logiciels / informatique
http://www.cs.virginia.edu/~lcc-win32


这篇关于将堆分配给不可用的函数静态变量。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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