怀疑Stack变量。 [英] Doubt on Stack variables.

查看:79
本文介绍了怀疑Stack变量。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



嗨在下面的函数中如何分配内存。


1)它是否会为所有char'分配内存一起或分配给

第一个字符。那么对于int然后是浮点数和

之后这个函数中只有第二个字符获取内存。

2)如果我声明变量,其中al变量的大小超出了

sizeof堆栈(假设它是1000)会发生什么?b $ b boud foo foo foo foo(int a,innt b)< br $>
{


char cb;

int ib;

float fc;

int ic;

char cc;


}

解决方案

>嗨在下面的函数中如何分配内存。


标准C没有规定如何。可能是通过采购订单

用PayPal支付。


C没有定义任何称为堆栈的东西。对于这个问题,

似乎是指分配自动变量的地方。


> 1)是否会分配内存所有的char都在一起或分配给第一个char。那么对于int然后是浮点数和

之后这个函数中只有第二个字符获取内存。



不能保证变量将被反转分配

将变量名称翻译成Pig Latin的顺序,或者

任何其他特定订单。


> 2)如果我声明变量,其中al变量的大小超出了
sizeof堆栈(假设它是1000)什么会发生什么?b $ b?



它可能不会很漂亮。期待超出这一点的任何事情

遭到破坏。当它被用于其原始目的时,期待一团糟。


> voud foo(int a,innt b)



voud? innt?这些typedef是否为双打?


> {


char cb;

int ib ;

float fc;

int ic;

char cc;

}


" deepak" < de ********* @ gmail.comwrites:


Hi在以下函数中如何分配内存。


1)它会为所有char'分配内存还是分配给
first char。那么对于int然后是浮点数和

之后这个函数中只有第二个字符获取内存。

2)如果我声明变量,其中al变量的大小超出了

sizeof堆栈(假设它是1000)会发生什么?b $ b boud foo foo foo foo(int a,innt b)< br $>
{


char cb;

int ib;

float fc;

int ic;

char cc;


}



编译器可以分配它喜欢的任何顺序的变量。如果你用
写出好的可移植代码,那就无所谓了。


C标准并没有指定堆栈。 (这个词在标准中甚至没有出现

)。大多数编译器确实使用堆栈来分配

局部变量,但即使如此,sizeof stack也是如此。并不意味着什么。


内存不是无限的,所以最终程序可能会耗尽空间

来分配局部变量。该限制通常适用于总额

当前堆栈。为所有活动功能分配,而不是为单个功能分配空间

。如果超出该限制(堆栈

溢出),则行为未定义;通常这意味着

程序终止并显示错误消息。


-

Keith Thompson(The_Other_Keith) ks *** @ mib.org < http://www.ghoti.net/~kst>

San迭戈超级计算机中心< *< http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。


" deepak" < de ********* @ gmail.comwrote in message

news:11 ********************* *@q2g2000cwa.googlegro ups.com ...


>

嗨在以下函数中如何分配内存。


1)它会为所有char'分配内存还是分配给
first char。那么对于int然后是浮点数和

之后这个函数中只有第二个字符获取内存。

2)如果我声明变量,其中al变量的大小超出了

sizeof堆栈(假设它是1000)什么'

发生了什么?


voud foo(int a,innt b)



我想这构成两个错别字?这是正确的吗?


void foo(int a,int b)


{


char cb;

int ib;

float fc;

int ic;

char cc ;


}



任何分配顺序都可以。

如果您的变量已消耗所有堆栈空间,你仍然想要更多,你

可能会得到一个堆栈外空间或类似的消息,然后你的程序崩溃了

,虽然有些系统甚至可能不会那么慷慨给你

的消息,并且不会崩溃只有你的计划。


-

世界上很容易接受世界的观点;孤独轻松

住在我们自己之后;但伟大的人就是在人群中间的人。

保持完美的甜蜜独处的孤独。

Ralph Waldo Emerson,自力更生1841年/> http://pinpoint.wordpress.com/



Hi In the following function how the memory ''ll be allocated.

1) Will it allocate memory for all the char''s together or allocate for
first char. then for int then for float and after
this only second char in the function gets memory.
2) If i declare variables where the size of al variables gone beyond
sizeof stack (assume it is 1000) what ''ll
happen?

voud foo( int a, innt b)
{

char cb;
int ib;
float fc;
int ic;
char cc;

}

解决方案

>Hi In the following function how the memory ''ll be allocated.

Standard C does not dictate how. It might be by purchase order
paid for with PayPal.

C does not define anything called a "stack". For this question,
it seems to refer to the place where auto variables are allocated.

>1) Will it allocate memory for all the char''s together or allocate for
first char. then for int then for float and after
this only second char in the function gets memory.

It is not guaranteed that the variables will be allocated in reverse
order of the translation of the variable name into Pig Latin, or
any other particular order.

>2) If i declare variables where the size of al variables gone beyond
sizeof stack (assume it is 1000) what ''ll
happen?

It probably won''t be pretty. Expect whatever''s beyond that point to
get trashed. Then expect a mess when it''s used for its original purpose.

>voud foo( int a, innt b)

voud? innt? Are these typedefs for doubles?

>{

char cb;
int ib;
float fc;
int ic;
char cc;

}



"deepak" <de*********@gmail.comwrites:

Hi In the following function how the memory ''ll be allocated.

1) Will it allocate memory for all the char''s together or allocate for
first char. then for int then for float and after
this only second char in the function gets memory.
2) If i declare variables where the size of al variables gone beyond
sizeof stack (assume it is 1000) what ''ll
happen?

voud foo( int a, innt b)
{

char cb;
int ib;
float fc;
int ic;
char cc;

}

The compiler can allocate the variables in any order it likes. If you
write good portable code, it won''t matter.

The C standard doesn''t specify a "stack" (the word doesn''t even appear
in the standard). Most compilers do use a stack for the allocation of
local variables, but even so "sizeof stack" doesn''t mean anything.

Memory is not infinite, so eventually a program can run out of space
to allocate local variables. The limit usually applies to the total
current "stack" allocation for all active functions, not to the space
for a single function. If that limit is exceeded (a "stack
overflow"), the behavior is undefined; usually this means that the
program terminates with an error message.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


"deepak" <de*********@gmail.comwrote in message
news:11**********************@q2g2000cwa.googlegro ups.com...

>
Hi In the following function how the memory ''ll be allocated.

1) Will it allocate memory for all the char''s together or allocate for
first char. then for int then for float and after
this only second char in the function gets memory.
2) If i declare variables where the size of al variables gone beyond
sizeof stack (assume it is 1000) what ''ll
happen?

voud foo( int a, innt b)

I suppose this constitutes two typos? Is this correct?

void foo(int a, int b)

{

char cb;
int ib;
float fc;
int ic;
char cc;

}

Any order of allocation is possible.
If your variables have consumed all stack space and you still want more, you
might get an "out of stack space" or similar message, followed by the crash of
your program, although some systems might not even be as generous as to give you
the message, and won''t crash only your program.

--
"It is easy in the world to live after the world''s opinion; it easy in solitude
to live after our own; but the great man is he who in the midst of the crowd
keeps with perfect sweetness the independence of solitude."
Ralph Waldo Emerson, Self-reliance 1841
http://pinpoint.wordpress.com/


这篇关于怀疑Stack变量。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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