?
Solved

Switch statement Vs function pointers

Posted on 2003-03-10
8
Medium Priority
?
347 Views
Last Modified: 2012-05-07
Are there any advantages of using an array of function pointers instead of switch statements with the function calls within them?
0
Comment
Question by:arut
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 11

Accepted Solution

by:
KurtVon earned 375 total points
ID: 8105212
Um, let's see.

1. It's much faster.

2. You can change what values map to what functions on the fly.

3. It makes more compact code.


And why you shouldn't:

1. It's not very flexible (all functions must have the same set of parameters).

2. It's not very readable.

3. Initization of the array can be a real pain.

Personally, reason 2 is the only thing that would convince me to do it.  Hope that helps.
0
 
LVL 6

Expert Comment

by:gj62
ID: 8105254
I'd agree with Kurt, except that unless you are making heavy use and calling ALOT of functions, the speed you gain with an array of function pointers isn't going to be seen...

I always take readability/maintainability over code compactness...  Just comes from working on larger projects with more than 1 person involved...
0
 
LVL 22

Expert Comment

by:grg99
ID: 8106622
The array of function pointers is going to be a tad faster, but it rarely matters if the function is more than a statement or two.

The downside is if you forget to initialize one of the pointers, or if your index goes outside the array bounds, BOOM!

I would use a switch statement, unless the number of functions got to be more than 15 or so, then the function array would be less visual clutter, although harder to declare and initialize.



0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:arut
ID: 8108891
How is it faster?

I need to do a lookup anyway!
0
 

Author Comment

by:arut
ID: 8108902
Sorry, I had a table lookup in mind.

Is it faster because you get direct access here
and in the case of a switch one has to make a comparison
with each case before executing a particular case statement?
0
 

Expert Comment

by:plushey
ID: 8109763
It is faster because you have direct access to the function pointer, there are no comparisons to be made at all. Obviously the number of functions you have will affect the average number of comparisons that will be made, and thus the speed issue becomes more important as the number of functions increases.

A switch statement is clearer and more readable except when the number of functions is high. I don't see declaration and initialisation of an array to be that hard. You can use variable argument lists with your functions so the number of parameters passed to them can be flexible but a switch statement would be easier to read and code.

You can pass an array of pointers to a function, you can't do that with a switch statement.
0
 
LVL 5

Expert Comment

by:Kocil
ID: 8110940
KurtVon answer is excelent. I just add two cases where function pointers is a MUST and switch is useless.
1. When the number of functions is dynamic, always changing at run time (You can't rewrite switch ot the fly, can't you ?). Try to build a state machine editor, and you will know what I mean.

2. When the priority of the functions is dynamic. With function pointer, you can move the high priority function to the front and will be checked earlier. You can't change the switch test order at run time. This is important on rel-time application.

Cheers.




0
 

Author Comment

by:arut
ID: 8117221
Actually if there is a way to share points, I would have given points to Kocil too. He gave some good examples!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question