临时记忆 [英] scratch memory

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

问题描述

为了避免在函数中声明静态变量,我被要求

来写一个临时存储器。

在函数外部保留一块内存并分配指针

为了方便而访问内存位置并访问它们。

我被告知这可以节省一些内存。我不明白这背后的逻辑

,因为我已经将变量声明为全局变量(假设我已经将
声明为main()中的块)这将永远是残差

的数据在prog的任何一点访问。对静态变量。哪个wud b

只有在它进入功能时才可以访问。


如果有人写过临时存储器请确认我的

理解是正确的,也是它的用法。


----


也是了解如何处理内存的任何链接一个

c-program`s(在windows上)执行b b hlp。


再见,

快点。

In order to avoid declaring static variables in a function I was asked
to write a scratch memory.
Reserve a block of memory outside the function and assigning pointers
to the memory locations as per convenience and access them.
I was told this would save some memory. I dont understand the logic
behind this, as i`ve declared variables as global (assuming i`ve
declared the block in main() ) this would always b a residual data for
access at any point of the prog. against static var. which wud b
available for access only whn it enters the function.

If anyone has ever written a scratch memory pls confirm if my
understanding is right and its use too.

----

also any link to understand on how memory is handled during a
c-program`s (on windows) execution would b of hlp.

bye,
hurry.

推荐答案

hurry写道:
为了避免在函数中声明静态变量我被问到了
写一个临时存储器。
在函数外部保留一块内存,并根据方便将指针分配到内存位置并访问它们。
我被告知这可以节省一些内存。我不明白这背后的逻辑,因为我已经将变量声明为全局变量(假设我已经在main()中声明了这个块),这将总是为了访问
前卫的任何一点。对静态变量。哪个wud b
只有当它进入功能时才可以访问。

如果有人写过一个临时记忆,请确认我的理解是否正确以及它的用途。


所有程序的静态对象(无论是在内部

还是在函数之外)都存在并占用他们的记忆

在整个计划期间。如果程序

实际上并不需要同时使用它们,那么

可能会浪费内存。例如,想象

一个以阶段:进行的程序。它初始化,它b / b
执行其计算的第一部分,第二部分......

然后它会写出结果,清理并终止。

如果有功能仅在N阶段使用且

不在任何早期或后期阶段,那么他们的

静态变量占用的内存将被浪费,除非N阶段是实际运行的



然而,使用动态分配的内存有足够的陷阱,并提供很多机会你可以通过调试器获得练习

...在开始使用动态内存替换静态之前,你需要

应该评估实际上有多少浪费,是否在物联网大事中重要。定期更换

您的汽车机油可以帮助您增加汽油里程,但每天更换它是一种虚假经济。

也任何链接,以了解如何处理内存在Windows程序执行期间(在Windows上)hlp。
In order to avoid declaring static variables in a function I was asked
to write a scratch memory.
Reserve a block of memory outside the function and assigning pointers
to the memory locations as per convenience and access them.
I was told this would save some memory. I dont understand the logic
behind this, as i`ve declared variables as global (assuming i`ve
declared the block in main() ) this would always b a residual data for
access at any point of the prog. against static var. which wud b
available for access only whn it enters the function.

If anyone has ever written a scratch memory pls confirm if my
understanding is right and its use too.
All of a program''s static objects (whether inside
or outside of functions) exist and occupy their memory
for the entire duration of the program. If the program
doesn''t actually need all of them simultaneously, that
might amount to a waste of memory. For example, imagine
a program that proceeds in "phases:" it initializes, it
does the first, second, ... parts of its calculation,
then it writes its results, cleans up, and terminates.
If there are functions that are used only in Phase N and
not in any earlier or later phases, the memory their
static variables occupy is wasted except while Phase N is
actually running.

However, using dynamically-allocated memory has plenty
of pitfalls and offers many chances for you to gain practice
with the debugger ... Before embarking on some kind of
wholesale replacement of static with dynamic memory, you
should assess just how much waste there actually is, whether
that''s important in the Grand Scheme of Things. Changing
your car''s motor oil regularly can help your gas mileage,
but changing it every day is a false economy.
also any link to understand on how memory is handled during a
c-program`s (on windows) execution would b of hlp.




每个数据对象在C具有存储持续时间,或松散地说b $ b说生命周期。有三个这样的:


- 静态存储持续时间:数据对象存在且

占用整个程序执行的内存,

如上所述。恕我直言,静态存储应该是少量使用的,特别是静态存储与外部

连接(全局变量)。


- 自动存储持续时间:数据对象属于
到可执行代码块(函数或函数中的子b / b块)。对象的生命周期是

与块的执行有关:当块

处于活动状态时,该对象存在并占用内存,但是

当执行离开块时对象消失

并且其内存可用于其他对象

占用。如果以递归方式输入一个块,则会创建一整套新的块本地对象,占用与外部块不同的内存



- 动态存储持续时间:您可以明确控制对象的生命周期,授予内存和撤消

无论何时选择。你使用malloc()或

calloc()或realloc()来分配内存,并使用free()

或realloc()来释放它。这是最灵活的

方法,也是最容易出错的方法。


C的具体实现可能需要自由的
$ b上面列出的$ b计划,只要符合规定的计划不能说b
告诉他们他们已经作弊。例如,一个实现

可能决定在输入块之前为自动变量分配内存,并保留内存一段时间

执行后离开块:


void func(void){

{

/ * subordinate block 1 * /

int inner1 = 42;

...

}

{

/ *从属区块2 * /

double inner2 = 42.0;

...

}

}


有些实现可能会在调用func()后立即为inner1

和inner2分配内存,而无需等待下属的
要输入的块。这样的实现

也可能会保留内存,直到func()返回,

即使(原则上)inner1和inner2不再存在
$ b他们的积木结束时$ b。这样的恶作剧被允许在

的效率名称中,但不能依赖 - 例如,

例如,这将是一个错误:


void func(void){

int * ptr;

{

/ * subordinate block * /

int inner = 42;

ptr =& inner;

...

}

printf("%d \ n",* ptr); / *错了! * /

}


....因为当从属块结束时,内存分配给内存的内存可能是(在原则,确实消失,所以

ptr发现自己指向不再存在的东西。


要弄清楚编译器是否碰巧使用

会做这种事情,在什么情况下,请查阅

其文档。但更明智的做法可能是故意无知,以免你开始混淆当前编译器与C语言相关的内容

承诺它会做。升级到更新版本的编译器,

和所有未承诺的行为都可能会发生变化。


-

Eric Sosman
es*****@acm-dot-org.inva lid



Each data object in C has a "storage duration," or loosely
speaking a "lifetime." There are three such:

- Static storage duration: The data object exists and
occupies memory for the entire execution of the program,
as described above. Static storage should, IMHO, be
used sparingly, especially static storage with external
linkage ("global variables").

- Automatic storage duration: The data object "belongs"
to a block of executable code (a function, or a sub-
block within a function). The object''s lifetime is
tied to the execution of the block: while the block
is active, the object exists and occupies memory, but
when execution leaves the block the object disappears
and its memory becomes available for other objects to
occupy. If a block is entered recursively, a whole
new set of block-local objects is created, occupying
memory that is distinct from that of the outer blocks.

- Dynamic storage duration: You control the lifetime of
the object explicitly, granting it memory and rescinding
the grant whenever you choose. You use malloc() or
calloc() or realloc() to allocate the memory, and free()
or realloc() to release it. This is the most flexible
method, and also the most error-prone.

Specific implementations of C may take liberties with the
schemes outlined above, so long as a conforming program cannot
tell that they''ve cheated. For example, an implementation
might decide to allocate memory for an auto variable before
its block is entered, and retain the memory for some time
after execution leaves the block:

void func(void) {
{
/* subordinate block 1 */
int inner1 = 42;
...
}
{
/* subordinate block 2 */
double inner2 = 42.0;
...
}
}

Some implementations might allocate memory for both inner1
and inner2 as soon a func() is called, without waiting for
their subordinate blocks to be entered. Such implementations
are also likely to keep that memory around until func() returns,
even though (in principle) inner1 and inner2 cease to exist
when their blocks end. Such shenanigans are permitted in the
name of efficiency, but are not to be relied upon -- for
example, this would be an error:

void func(void) {
int *ptr;
{
/* subordinate block */
int inner = 42;
ptr = &inner;
...
}
printf ("%d\n", *ptr); /* WRONG! */
}

.... because when the subordinate block finishes, the memory
allocated to inner may (in principle, "does") disappear, so
ptr finds itself pointing at something that no longer exists.

To find out whether the compiler you happen to be using
does this sort of thing, and under what circumstances, consult
its documentation. But the wiser course may be to remain
intentionally ignorant, lest you start confusing what your
current compiler happens to do with what the C languages
promises it will do. Upgrade to a newer version of the compiler,
and all the non-promised behaviors are subject to change.

--
Eric Sosman
es*****@acm-dot-org.invalid


在文章< 11 ********************** @ z14g2000cwz.googlegroups中。 com>,

hurry< hu ********* @ gmail.com>写道:
In article <11**********************@z14g2000cwz.googlegroups .com>,
hurry <hu*********@gmail.com> wrote:
为了避免在函数中声明静态变量,我被要求写一个临时存储器。
在函数外部保留一块内存并指定指针
根据方便进入内存位置并访问它们。
我被告知这可以节省一些内存。我不明白这背后的逻辑,因为我已经将变量声明为全局变量(假设我已经在main()中声明了这个块),这将总是为了访问
前卫的任何一点。对静态变量。哪个wud b
只有在进入函数时才可以访问。
In order to avoid declaring static variables in a function I was asked
to write a scratch memory.
Reserve a block of memory outside the function and assigning pointers
to the memory locations as per convenience and access them.
I was told this would save some memory. I dont understand the logic
behind this, as i`ve declared variables as global (assuming i`ve
declared the block in main() ) this would always b a residual data for
access at any point of the prog. against static var. which wud b
available for access only whn it enters the function.




数据是否可访问,如果声明为静态

然后它将继续存在而其他函数正在运行。

在函数中使用静态变量是因为没有一种方法可以节省空间,而不是使用静态变量

的程度可能会让你避免将引用传递给内存

通过一些调用层。


如果我理解你所写的正确,那么你做出这个

请求的人正在处理另一件事,那就是

如果你有静态记忆是分配了许多不同的
函数,静态内存不一定都需要同时存在

。另一张海报提到了阶段:你可能

需要一个变量设置为程序的第一部分活着,

但是它可能不再是需要但可能需要一个不同的变量集

。如果你正在使用临时记忆你在上面描述的形式,然后在第一阶段结束后,你可以为了第二个目的重新使用静态内存块,而不是


必须分配更多内存。所需的内存总量

然后成为必须同时存在于各个点的静态变量

的大小总和的最大值,而不是

程序执行中某些时候可能存在的静态变量大小的大小总和




沿着相同的行:有时为方便起见,存在静态内存

以防止重新计算值。当你在一段需要重复计算值的代码
时,你保持静态内存为
,但那之前可能存在明显的时间间隔

您需要再次使用这些值。如果重新计算的价值较低,那么昂贵的价格就会降低。比那段时间内保留它们的内存成本,

那么冲洗它们可能是有意义的,重新使用内存来获取某些东西

else,然后稍后重新计算它们。


随机示例:如果您正在为手机编程游戏,那么在游戏本身启动之前

,您可能正在滚动重复列表

高分,直到用户触发开始。可显示的

版本的分数可能与内部

表示不完全相同,特别是如果分数是以缓慢访问的方式持有

低功耗内存,而不是更高电流的快速RAM。所以你可能会把显示分数放入静态持续时间的内存中。一旦用户启动游戏,你就不再需要那个记忆了,而不是

,直到用户完成游戏并且是时候显示新的

高分榜。当重新计算的成本不高时,保持整个游戏中的静态内存将是一种浪费。

-

没有人有权通过要求经验证据来摧毁另一个人的信念。 - Ann Landers



Whether the data is accessible or not, if it is declared static
then it will continue to exist while other functions are running.
Using static variables in functions is therefor not a method of
saving space, other than to the extent that using a static variable
might allow you to avoid passing the reference to the memory
down through a number of calling layers.

If I understand what you have written correctly, whoever made this
request of you is dealing with a different matter, which is that
if you have static memory being allocated in a number of different
functions, the static memory does not necessarily all need to exist
at the same time. The other poster referred to "phases": you might
need a variable set to be alive for the first part of a program,
but then it might no longer be needed but a different variable set
might be needed. If you are using "scratch memory" of the form you
describe above, then after the end of the first phase, you can
reuse the block of static memory for the second purpose, without
having to allocate more memory. The total amount of memory required
then becomes the maximum of the sums of the sizes of the static variables
that must simultaneously exist at various points, rather than the
sums of the sizes of the sizes of the static variables that might exist
at some point in program execution.

Along the same lines: sometimes static memory exists for convenience
to prevent having to recalculate values. While you are in a section
of code that needs the calculated values repeatedly, you hold on to
the static memory, but there then might be a noticable time gap before
you need the values again. If the recalculation of the values is less
"expensive" than the cost of memory to hold on to them for that time,
then it might make sense to flush them, re-use the memory for something
else, and then recalculate them later.

Random example: if you are programming a game for a cell phone, then
before the game itself starts, you might be scrolling the list of
high scores repeatedly until the user triggers a start. The displayable
version of the scores is likely not exactly the same as the internal
representation, especially if the scores are held in a slow-access
low-power memory rather than in higher-current fast RAM. So you might
put the displayble scores into memory of static duration. And once
the user starts the game, you don''t need that memory anymore, not
until the user finishes the game and it is time to display the new
high-score list. It would be a waste to hold on to that static memory
all through the game, when the cost of recalculating it is not high.
--
"No one has the right to destroy another person''s belief by
demanding empirical evidence." -- Ann Landers


对这个问题的良好回应。 划痕记忆更常见的是在嵌入式体系结构上运行的应用程序中使用
,其中内存需求是一个主要问题。在缺乏

MMU的环境中,管理自己的内存变得必要。这些情况确实不会很好地使用静态变量,这些变量一直保留在
内存中。支持需求的环境

分页/虚拟内存将透明地优化内存使用,

允许自由使用静态。由于这在许多嵌入式设备中是不可能的,因此工作留给程序员。
Good responses to this question. "Scratch memory" is more commonly
used in applications that is run on embedded architectures, where
memory requirements are a major concern. In environments lacking a
MMU, managing one''s own memory becomes necessary. These situations do
not lend themselves well to using static variables, which remain in
memory persistently. An environnment that supports demand
paging/virtual memory would optimize memory usage transparently,
allowing for liberal use of statics. Since this is not possible in
many embedded devices, the job is left to the programmer.

数据是否可访问,如果它被声明为静态
那么它将在其他函数运行时继续存在。
在函数中使用静态变量不是一种节省空间的方法,而不是使用的程度一个静态变量
可能会让你避免将引用传递给内存
通过一些调用层。

Whether the data is accessible or not, if it is declared static
then it will continue to exist while other functions are running.
Using static variables in functions is therefor not a method of
saving space, other than to the extent that using a static variable
might allow you to avoid passing the reference to the memory
down through a number of calling layers.




我不确定你的意思是什么在

函数内声明的静态变量仍然保留在此函数范围内;应用于其他自动变量的静态

关键字不会更改该变量的

范围。也许我不明白你的意思?



I am not sure what you mean by this. Static variables declared inside
of functions still remain scoped within this function; the static
keyword applied to otherwise automatic variables does not change the
scope of that variable. Maybe I''m not understanding what you mean?


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

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