Re: allocating space for local variables
- From: Jack Klein <jackklein@xxxxxxxxxxx>
- Date: Mon, 26 Jun 2006 10:42:02 -0500
On 26 Jun 2006 07:48:31 -0700, "deepak" <deepakpjose@xxxxxxxxx> wrote
in comp.lang.c:
Nils O. Selåsdal wrote:
junky_fellow@xxxxxxxxxxx wrote:
Hi guys,
Consider the following piece of code:
func()
{
if(x==0) {
int arr[50];
/* do something */
}
else {
int arr2[100];
/* do something */
}
}
I wanted to know, if the space of arr[50 is allocated only when the
condition "x==0" is
true or the space is allocated when the function "func" is called.
I am working on an m68k implementation where the local varialbles are
allocated
on stack. I would be really grateful if anyone can provide me an
answer for an m68k
The answer would be compiler dependant, compiler flags dependant, and
code dependant. Some code , depending on the optimizations/etc. might be
reordered to play things out very differently than just some very
slightly different code.
Please correct me if i'm wrong.
Consider yourself corrected.
Memory will be allocated for both arrays since it happens at
compilation time.
That is true for objects with static storage duration, specifically
all objects defined at file scope or with the 'static' keyword.
Automatic objects do not need to be allocated until the block
containing their definition is entered.
Here malloc comes into picture. It 'll do it at run time.
Once again i'm telling it's only my understanding. If wrong correct me.
Your best bet is to peek at the assembly(if available - gcc -S for gcc)
generated for the exact code you have compiled with the options you'll
be using in "production".
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
.
- References:
- allocating space for local variables
- From: junky_fellow@xxxxxxxxxxx
- Re: allocating space for local variables
- From: "Nils O. Selåsdal"
- Re: allocating space for local variables
- From: deepak
- allocating space for local variables
- Prev by Date: Re: allocating space for local variables
- Next by Date: Re: automated coding standard validation?
- Previous by thread: Re: allocating space for local variables
- Next by thread: Re: allocating space for local variables
- Index(es):
Relevant Pages
|
|