CLARIFICATION - Variable Declaration & Memory

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Anoop

    CLARIFICATION - Variable Declaration & Memory

    Hi guys

    i have a piece of code

    main()
    {
    switch(...)
    {
    case 1:
    {
    char a[200];
    }
    case 2:
    {
    char a[200];
    }
    case 3:
    {
    char a[200];
    }
    }
    }

    now the question is what will be the memory allocated when the program
    enters his function to the first statement switch(...)

    1. will it be 600 bytes - sum of all local variables
    2. or the allocation will be done depending upon execution path and in
    whichever case it goes.


    Whatever is the answer? Kindly give proper reasons why will that
    happen and also any compiler dependebt issues.

    Thanx in Advance

    Anoop
  • Jerry Coffin

    #2
    Re: CLARIFICATION - Variable Declaration & Memory

    In article <7024bbd3.03070 92040.2a9873b1@ posting.google. com>,
    ahuja_anoop@yah oo.co.uk says...[color=blue]
    > Hi guys
    >
    > i have a piece of code
    >
    > main()
    > {
    > switch(...)
    > {
    > case 1:
    > {
    > char a[200];
    > }
    > case 2:
    > {
    > char a[200];
    > }
    > case 3:
    > {
    > char a[200];
    > }
    > }
    > }
    >
    > now the question is what will be the memory allocated when the program
    > enters his function to the first statement switch(...)
    >
    > 1. will it be 600 bytes - sum of all local variables
    > 2. or the allocation will be done depending upon execution path and in
    > whichever case it goes.[/color]

    It depends on the compiler. With most I've used, 200 bytes would be
    allocated. Since only one 'a' can be in scope at any given time, that
    memory can be safely used for all of them at the same time.

    There's no guarantee of this though -- the compiler _could_ allocate
    space for each branch separately. Since only one of them is in scope at
    a time, even attempting to detect whether they use the same storage
    almost inevitably ends up depending on undefined behavior.

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.

    Comment

    • Thomas Matthews

      #3
      Re: CLARIFICATION - Variable Declaration &amp; Memory

      Anoop wrote:[color=blue]
      > Hi guys
      >
      > i have a piece of code
      >
      > main()
      > {
      > switch(...)
      > {
      > case 1:
      > {
      > char a[200];
      > }
      > case 2:
      > {
      > char a[200];
      > }
      > case 3:
      > {
      > char a[200];
      > }
      > }
      > }
      >
      > now the question is what will be the memory allocated when the program
      > enters his function to the first statement switch(...)
      >
      > 1. will it be 600 bytes - sum of all local variables
      > 2. or the allocation will be done depending upon execution path and in
      > whichever case it goes.
      >
      >
      > Whatever is the answer? Kindly give proper reasons why will that
      > happen and also any compiler dependebt issues.
      >
      > Thanx in Advance
      >
      > Anoop[/color]

      1. The declaration and definition of the arrays is local to the block
      it is contained in. When execution leaves the block, the local
      variables disappear. The block is defined between the open and
      close braces: '{' and '}'.

      2. Since there is no 'break' after any of the cases, the torment that
      the memory allocation goes through depends upon the switch value.
      In the above case, if the switch variable evaluates to 1, there
      will be 3 times that an array is locally allocated and removed.

      3. The compiler may choose not to allocate the array since it is not
      used anywhere else in the block that it was defined in.

      4. Since there is no code in any of the case statements, the compiler
      may decide to not translate the switch statement (an optimization),
      thus no arrays would be allocated.

      5. Defining variables in a case statement or block and using them
      outside of the switch statement is bad karma. Better karma is
      to define variables before the switch statement, then process
      them in the case(s) and maybe also after the switch statement.

      --
      Thomas Matthews

      C++ newsgroup welcome message:

      C++ Faq: http://www.parashift.com/c++-faq-lite
      C Faq: http://www.eskimo.com/~scs/c-faq/top.html
      alt.comp.lang.l earn.c-c++ faq:

      Other sites:
      http://www.josuttis.com -- C++ STL Library book
      http://www.sgi.com/tech/stl -- Standard Template Library

      Comment

      • Anoop

        #4
        Re: CLARIFICATION - Variable Declaration &amp; Memory

        Thomas Matthews <tomatthews@sbc global.net> wrote in message news:<3F0D4B65. 3010800@sbcglob al.net>...[color=blue]
        > Anoop wrote:[color=green]
        > > Hi guys
        > >
        > > i have a piece of code
        > >
        > > main()
        > > {
        > > switch(...)
        > > {
        > > case 1:
        > > {
        > > char a[200];
        > > }
        > > case 2:
        > > {
        > > char a[200];
        > > }
        > > case 3:
        > > {
        > > char a[200];
        > > }
        > > }
        > > }
        > >
        > > now the question is what will be the memory allocated when the program
        > > enters his function to the first statement switch(...)
        > >
        > > 1. will it be 600 bytes - sum of all local variables
        > > 2. or the allocation will be done depending upon execution path and in
        > > whichever case it goes.
        > >
        > >
        > > Whatever is the answer? Kindly give proper reasons why will that
        > > happen and also any compiler dependebt issues.
        > >
        > > Thanx in Advance
        > >
        > > Anoop[/color]
        >
        > 1. The declaration and definition of the arrays is local to the block
        > it is contained in. When execution leaves the block, the local
        > variables disappear. The block is defined between the open and
        > close braces: '{' and '}'.
        >
        > 2. Since there is no 'break' after any of the cases, the torment that
        > the memory allocation goes through depends upon the switch value.
        > In the above case, if the switch variable evaluates to 1, there
        > will be 3 times that an array is locally allocated and removed.
        >
        > 3. The compiler may choose not to allocate the array since it is not
        > used anywhere else in the block that it was defined in.
        >
        > 4. Since there is no code in any of the case statements, the compiler
        > may decide to not translate the switch statement (an optimization),
        > thus no arrays would be allocated.
        >
        > 5. Defining variables in a case statement or block and using them
        > outside of the switch statement is bad karma. Better karma is
        > to define variables before the switch statement, then process
        > them in the case(s) and maybe also after the switch statement.
        >
        > --
        > Thomas Matthews
        >
        > C++ newsgroup welcome message:
        > http://www.slack.net/~shiva/welcome.txt
        > C++ Faq: http://www.parashift.com/c++-faq-lite
        > C Faq: http://www.eskimo.com/~scs/c-faq/top.html
        > alt.comp.lang.l earn.c-c++ faq:
        > http://www.raos.demon.uk/acllc-c++/faq.html
        > Other sites:
        > http://www.josuttis.com -- C++ STL Library book
        > http://www.sgi.com/tech/stl -- Standard Template Library[/color]


        Hi

        thanx for ur reply

        i wud like to add something here is that i m using Metrowerks
        CodeWarrior.

        and when i debug the module i have written as above it allocates all 3
        char arrays at once that wastes a lot of memory say 400 bytes in the
        above example.

        The question actually boils down to that it is taking 600 bytes but
        WHY?

        The compiler shud allocate memory for only to those variables that are
        in a execution path OR as and when they r required. is there any
        problem that the compiler might face if it doesnt allocate evrything
        in the starting itself.

        how can, if possible a compiler be configured to behave otherwise, say
        allocating variables only when they are required.

        Thanx in advance

        Regards
        ANOOP

        Comment

        Working...