动态内存分配堆栈 [英] Dynamic memory allocation stack heaps

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

问题描述

我对动态内存分配和堆栈和堆有疑问


在哪里使用动态内存分配?


函数调用有一些柜台,如i,在下面

函数中。这是存储在堆栈中。如果是,它是否会被删除

退出该功能。


是为此目的所需的动态内存分配


int listnum()

{

unsigned char i;

for(i = 0; i> 10; i ++)

}

解决方案

vivek写道:


[后我想知道这是否是作业。哦好吧。]


i对动态内存分配和堆栈和堆有一些疑问


这里是使用的动态内存分配?



静态或堆栈(在C中,auto [matic])分配是不合适的:

例如所需的商店大小事先不知道,或者内存必须是

可用的时间超过分配

的功能的生命周期。


["动态]分配是模棱两可的。 Stack又称自动分配是动态的;

变量在输入函数时就会存在,并且当它退出时不再可访问
。人们经常,但并非总是如此,当他们说

动态时,
意味着类似malloc / free或堆分配。


在函数调用中有一些计数器,如i,在下面

函数中。这是存储在堆栈中。如果是,它是否会被删除

退出该功能。


是为此目的所需的动态内存分配


int listnum()

{

unsigned char i;

for(i = 0; i> 10; i ++)



糟糕,缺少循环体。我们假装你在`)之后写了`{}`。


}



变量`i`是自动的,并且对于执行的执行是本地的`listnum`。

典型的实现会将它分配到堆栈上,但这并不是标准所要求的
,只是方便。所以它是动态内存

分配 - 只是不/非常/动态。


当函数退出时,变量变得无法访问。 (也就是说,

如果你试图访问它会发生什么 - 例如通过你可以访问变量时得到的指针

- 未定义,这意味着

你不知道将会发生什么。)是否算作变量

被删除。取决于你所说的删除。通常,用于该变量版本的

商店将用于

else。


-

Chris静态对芯片不利 - 因此醋 Dollin


Hewlett-Packard Limited注册号:

注册办事处:Cain Road,Bracknell,Berks RG12 1HN 690597英格兰


11月15日下午4:27,Chris Dollin< chris.dol ... @ hp.comwrote:


vivek写道:


[事后我不知道这是否是作业。哦好吧。]


i对动态内存分配和堆栈以及堆积有一些疑问


在哪里使用动态内存分配?



静态或堆栈(在C中,auto [matic])分配是不合适的:

例如所需的商店大小事先不知道,或者内存必须是

可用的时间超过分配

的功能的生命周期。


["动态]分配是模棱两可的。 Stack又称自动分配是动态的;

变量在输入函数时就会存在,并且当它退出时不再可访问
。人们经常,但并非总是如此,当他们说

动态时,
意味着类似malloc / free或堆分配。


在函数调用中有一些计数器,如i,在下面

函数中。这是存储在堆栈中。如果是,是否将从函数中删除



是为此目的所需的动态内存分配


int listnum ()

{

unsigned char i;

for(i = 0; i> 10; i ++)


糟糕,缺少循环体。我们假装你在`)之后写了`{}`。


}



变量`i`是自动的,并且是`listnum`执行的本地。

典型的实现将它分配到堆栈上,但这不是$ /> $标准要求b $ b,方便。所以它是动态内存

分配 - 只是不/非常/动态。


当函数退出时,变量变得无法访问。 (也就是说,

如果你试图访问它会发生什么 - 例如通过你可以访问变量时得到的指针

- 未定义,这意味着

你不知道将会发生什么。)是否算作变量

被删除。取决于你所说的删除。通常,用于该变量版本的

商店将用于

else。


-

Chris静态对芯片不利 - 因此醋 Dollin


Hewlett-Packard Limited注册号:

注册办事处:Cain Road,Bracknell,Berks RG12 1HN 690597 England



谢谢。通常哪些数据存储在堆上。


vivek写道:


谢谢。通常哪些数据存储在堆上。



无法静态或自动分配的数据。我不确定

什么是通常会在这里。


[这也取决于你的意思堆,因为标准不会说b
谈论一堆;你可以说在C中,堆由malloc / realloc / free管理的内存是
,这导致了由malloc或realloc分配的无数的答案数据
......]


-

Chris重言式是重言式 Dollin


Hewlett-Packard Limited注册号:

注册办事处:Cain Road,Bracknell,Berks RG12 1HN 690597英格兰


i have some doubts on dynamic memory allocation and stacks and heaps

where is the dynamic memory allocation used?

in function calls there are some counters like "i" in the below
function. Is this stored in stack. If yes whether it will be deleted
on exiting from the function.

is dynamic memory allocation needed for this purpose

int listnum()
{
unsigned char i;
for(i=0;i>10;i++)
}

解决方案

vivek wrote:

[Post-hoc I wonder if this is homework. Oh well.]

i have some doubts on dynamic memory allocation and stacks and heaps

where is the dynamic memory allocation used?

Where static or stack (in C, "auto[matic]") allocation isn''t appropriate:
eg the size of store needed isn''t known in advance, or the memory must be
available for longer than the lifetime of the function which allocated
it.

["Dynamic" allocation is ambiguous. Stack aka auto allocation is dynamic;
variables come into existence when their function is entered and are
no longer accessible when it exits. People often, but not invariably,
mean something like malloc/free or heap allocation when they say
"dynamic".]

in function calls there are some counters like "i" in the below
function. Is this stored in stack. If yes whether it will be deleted
on exiting from the function.

is dynamic memory allocation needed for this purpose

int listnum()
{
unsigned char i;
for(i=0;i>10;i++)

Oops, missing loop body. We''ll pretend you wrote `{}` after the `)`.

}

The variable `i` is automatic and is local to the execution(s) of `listnum`.
A typical implementation will allocate it on a stack, but this isn''t
required by the Standard, just convenient. So it''s dynamic memory
allocation -- just not /very/ dynamic.

When the function exits, the variable becomes inaccessible. (That is,
what happens if you try to access it -- eg through a pointer you got
when the variable was accessible -- is Not Defined, which means that
you Do Not Know what will happen.) Whether that counts as the variable
being "deleted" depends on what you mean by "deleted". Typically the
store used for that version of the variable will be used for something
else.

--
Chris "static is bad for chips -- hence vinegar" Dollin

Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England


On Nov 15, 4:27 pm, Chris Dollin <chris.dol...@hp.comwrote:

vivek wrote:

[Post-hoc I wonder if this is homework. Oh well.]

i have some doubts on dynamic memory allocation and stacks and heaps

where is the dynamic memory allocation used?


Where static or stack (in C, "auto[matic]") allocation isn''t appropriate:
eg the size of store needed isn''t known in advance, or the memory must be
available for longer than the lifetime of the function which allocated
it.

["Dynamic" allocation is ambiguous. Stack aka auto allocation is dynamic;
variables come into existence when their function is entered and are
no longer accessible when it exits. People often, but not invariably,
mean something like malloc/free or heap allocation when they say
"dynamic".]

in function calls there are some counters like "i" in the below
function. Is this stored in stack. If yes whether it will be deleted
on exiting from the function.

is dynamic memory allocation needed for this purpose

int listnum()
{
unsigned char i;
for(i=0;i>10;i++)


Oops, missing loop body. We''ll pretend you wrote `{}` after the `)`.

}


The variable `i` is automatic and is local to the execution(s) of `listnum`.
A typical implementation will allocate it on a stack, but this isn''t
required by the Standard, just convenient. So it''s dynamic memory
allocation -- just not /very/ dynamic.

When the function exits, the variable becomes inaccessible. (That is,
what happens if you try to access it -- eg through a pointer you got
when the variable was accessible -- is Not Defined, which means that
you Do Not Know what will happen.) Whether that counts as the variable
being "deleted" depends on what you mean by "deleted". Typically the
store used for that version of the variable will be used for something
else.

--
Chris "static is bad for chips -- hence vinegar" Dollin

Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England

Thanks. Typically which data are stored on the heap.


vivek wrote:

Thanks. Typically which data are stored on the heap.

Data that can''t be allocated statically or automatically. I''m not sure
what "typically" would be here.

[It also depends on what you mean by "the heap", since the Standard doesn''t
talk about a heap; you can say that in C, "the heap" is the memory managed
by malloc/realloc/free, which leads to the unilluminating answer "data
allocated by malloc or realloc" ...]

--
Chris "tautologies are tautologies" Dollin

Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England


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

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