CLARIFICATION - Variable Declaration & Memory

A

Anoop

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
 
J

Jerry Coffin

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.

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.
 
T

Thomas Matthews

Anoop said:
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

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.learn.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
 
A

Anoop

Thomas Matthews said:
Anoop said:
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

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.learn.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


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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,127
Messages
2,570,757
Members
47,315
Latest member
robertsoker

Latest Threads

Top