473,382 Members | 1,786 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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
Jul 19 '05 #1
3 3156
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

83
by: Alexander Zatvornitskiy | last post by:
Hello All! I'am novice in python, and I find one very bad thing (from my point of view) in language. There is no keyword or syntax to declare variable, like 'var' in Pascal, or special syntax in...
7
by: YGeek | last post by:
Is there any difference between declaring a variable at the top of a method versus in the code of the method? Is there a performance impact for either choice? What about if the method will return...
42
by: baumann | last post by:
hi all, typedef int (*pfunc)(int , int); pfunc a_func; i know it's ok, but how can define a_func without typedef statement? thanks .
10
by: ElanKathir .S.N | last post by:
Hi all ! VB.NET adds the ability to create variables that are visible only within a block. A block is any section of code that ends with one of the words End , Loop , or Next . This means that...
2
by: Workgroups | last post by:
I need some clarification with the whole "no pointers in VB.NET" thing, because it seems like there are "quazi-pointers" happening but I want to make sure I'm interpreting all this correctly. ...
10
by: mark | last post by:
I have this class: class Selections { OSStatus Init(); protected: CFMutableSetRef selectionObjects; static void CFASelectionsApplier(const void* value, void* ctx); OSType ready; public:...
22
by: James Brodie | last post by:
I just wanted to get some advice on what are the better practices: 1) Should variables be declared at the beginning of a function or just before they are required? 2) Should all variabled be...
2
by: Florian Loitsch | last post by:
hi, What should be the output of the following code-snippet? === var x = "global"; function f() { var x = 0; eval("function x() { return false; }"); delete x; alert(x); }
13
by: mdh | last post by:
FAQ 1.7 shows that, amongst other things, int i = 0; is a definition. Page 128 of K&R say: "A struct declaration defines a type. The right brace that terminates the list of members may...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.