By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,979 Members | 943 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,979 IT Pros & Developers. It's quick & easy.

CLARIFICATION - Variable Declaration & Memory

P: n/a
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
Jul 19 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
In article <70**************************@posting.google.com >,
ah*********@yahoo.co.uk says...
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.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 19 '05 #2

P: n/a
Anoop wrote:
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

Jul 19 '05 #3

P: n/a
Thomas Matthews <to********@sbcglobal.net> wrote in message news:<3F**************@sbcglobal.net>...
Anoop wrote:
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
Jul 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.