473,386 Members | 1,821 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,386 software developers and data experts.

What costs more space: a structure or switch statement?

What takes up more space: a structure or switch statement, with the same amount of members? I am assuming the space used is determined by the compiler; mine is gcc.

thanks!
May 17 '10 #1
8 1489
jkmyoung
2,057 Expert 2GB
? I think you're comparing apples to oranges.

the structure, since it needs to know what type and name of each of its members in addition to the storage space. A switch has all the same type of members with no names.
May 17 '10 #2
donbock
2,426 Expert 2GB
A structure is a data type that specifies the contents of a variable; a switch statement is executable code. I don't know how to compare them.

Please provide a short example of what you envision to be an equivalent structure and switch statement. I think that will help us answer you better.
May 17 '10 #3
@donbock
Expand|Select|Wrap|Line Numbers
  1. typedef struct s_nameTypes 
  2. {
  3.   int    id;
  4.   char *name;
  5. } NameTypes;
  6.  
  7. int apples = 0x1;
  8. int oranges = 0x2;
  9.  
  10. NameTypes names[] =
  11. {
  12.   { apples, "i am an apple" },
  13.   { oranges, "i am an orange" },
  14.   { 0x0, "i don't know what i am" }
  15. }
  16.  
  17. while( names[i] != 0x0 )
  18.   if( someID == names[i].id )
  19.      strcpy(myname, names[i].name);
  20.  
  21.  
vs. a switch statement whose cases are the IDs:

Expand|Select|Wrap|Line Numbers
  1. int apples = 0x1;
  2. int oranges = 0x2;
  3.  
  4. switch( someID )
  5. {
  6.   case oranges :
  7.       strcpy( myname, "I am an apple!");
  8.       break;
  9.  
  10.   case apples :
  11.       strcpy( myname, "I am an orange!");
  12.        break;
  13. }
  14.  

So, which one is better?
May 17 '10 #4
donbock
2,426 Expert 2GB
So you essentially have a database; id is the key field and name is an attribute field. Given an id value, you want to extract an attribute field from the corresponding database record. You're asking if it is better to have an array that explicitly models the database, or construct the database implicitly within your executable code.

In my opinion the most important measures of which approach is better are the following. The amount of space consumed is not very important.
  • easiest to understand
  • easiest to implement
  • easiest to maintain
  • most robust (hardest to break)
In my opinion an explicit database wins in every category.

By the way, I don't think the code you provided will work; but that doesn't change my conclusion.
May 17 '10 #5
@donbock
If the database contains a million entries then the space consumption is relatively important. Hence my question.
May 17 '10 #6
donbock
2,426 Expert 2GB
It is hard to believe you are contemplating a switch statement with a million cases.

Do you know ahead of time if any of those million records definitely won't need to be searched? Do you know ahead of time if any of the fields won't be used at all? If you can't rule out any of the records or fields then they will all be there one way or another, whether it is in an array or distributed throughout your code.

Execution time may be more important that space. Can you come up with a search algorithm that will find the matching record faster?
May 17 '10 #7
@donbock
Would you suggest using a hash table? Please share your experteese...
May 17 '10 #8
donbock
2,426 Expert 2GB
The most important thing is to not retype any of this million-record database. What form does it take (text file, binary file, initializer in a source file, etc.)? How many times do you have to search the database?
May 18 '10 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

35
by: Thomas Matthews | last post by:
Hi, My son is writing a program to move a character. He is using the numbers on the keypad to indicate the direction of movement: 7 8 9 4 5 6 1 2 3 Each number has a direction except...
2
by: MJ | last post by:
Hi We can use the switch statement, or if else statement instead of switch One more method is there which can replace the switch using the function pointer or the array of function pointer. If...
18
by: swaroophr | last post by:
Which of switch statement and if-else statement takes less time to execute?
2
by: Angel Of Death | last post by:
I have a method. It takes some XML as a parameter. Depending on the content of the XML it should create a specific object and call a KNOWN method. So: public void PersistXml(string XmlData){} ...
4
by: =?Utf-8?B?cm9nZXJfMjc=?= | last post by:
I need to convert text. lots of it.. like over 100 different characters... TextToConvert = TextToConvert.Replace("2", "2"); TextToConvert = TextToConvert.Replace("3", "3"); TextToConvert =...
2
by: Phillip B Oldham | last post by:
What would be the optimal/pythonic way to subject an object to a number of tests (based on the object's attributes) and redirect program flow? Say I had the following: pets = {'name':...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.