Re: allocating space for local variables



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
.



Relevant Pages

  • Re: allocating space for local variables
    ... int arr2; ... true or the space is allocated when the function "func" is called. ... The answer would be compiler dependant, compiler flags dependant, and ...
    (comp.lang.c)
  • Re: allocating space for local variables
    ... int arr2; ... true or the space is allocated when the function "func" is called. ... The answer would be compiler dependant, compiler flags dependant, and ...
    (comp.lang.c)
  • Re: The importance of prototypes
    ... In the first call to func, the int value 42 was passed to the ... With a proper prototype in scope, the compiler knows that func ...
    (comp.lang.c)
  • Re: Newbie: Linking troubles
    ... > Hi I am having trouble understanding how to link objects. ... > int func(); ... because it relates to your specific compiler suite. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: function templates are not being compiled !
    ... void f ... int main ... the compiler whether it diagnoses the error or not. ... dependant. ...
    (microsoft.public.dotnet.languages.vc)