473,404 Members | 2,178 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,404 software developers and data experts.

Function Ptrs a bad practice in c+ ?? Array od Function Ptr - Indexing issue

Hi
Am trying to replace the classic switch case construct (am workng on
a telecom stack devlpmnt)with an array of function ptrs.

The problm am facing is that of indexing. - the case vals can be
any of 0- 0xFF(1 byte long) - but unfortunately as of now I just need
to use about 15 of 'em.

In the sense I can't just make an array of function ptrs - coz the
case vals I need to use right now are high up in the range.

My soln was somethng like this

enumSomeError (*fn[])() = {NULL,NULL,... ,
decodeParamOne,NULL,...//etc};
....
// get rid of the switch
if (fn[getParamType])
fn[getParamType()};// etc

PROBLEM - since I've very few functions to be actually invoked in
the whole range, the array of function occupies quite a bit of memory -
most of which has null pointers.
Isn't there a better soln? ( I don't really want to use a Map or
any other built in c++ container)

Next , Is there really much of a performance diff betn function
pointers & switch?

Also - is the above a bad practice?? Experienced coders at my firm
say it's not a good design to use function pointers.

thanks a lot for your comments & help

Sep 19 '05 #1
1 2188

"jon wayne" <jo**********@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Hi
Am trying to replace the classic switch case construct (am workng on
a telecom stack devlpmnt)with an array of function ptrs.

The problm am facing is that of indexing. - the case vals can be
any of 0- 0xFF(1 byte long) - but unfortunately as of now I just need
to use about 15 of 'em.

In the sense I can't just make an array of function ptrs - coz the
case vals I need to use right now are high up in the range.

My soln was somethng like this

enumSomeError (*fn[])() = {NULL,NULL,... ,
decodeParamOne,NULL,...//etc};
....
// get rid of the switch
if (fn[getParamType])
fn[getParamType()};// etc

PROBLEM - since I've very few functions to be actually invoked in
the whole range, the array of function occupies quite a bit of memory -
most of which has null pointers.
That really shouts out for a map.
Isn't there a better soln? ( I don't really want to use a Map or
any other built in c++ container)
Why not? The standard container std::map isn't built in anyway. A
std::map<CaseType, boost::function<void(void)> > will get you out of a lot
of hassal while retaining the efficiency. If you are uneasy, why not tell us
why you don't want to use standard containers.

If you feel a map is slow, i would argue that a switch/case is equally slow,
dispite its appearance. Better still, if you have a large number of entries,
you can use a hash map. But my opinion is with 15 entries std::map is still
the best performing option.

Next , Is there really much of a performance diff betn function
pointers & switch?
There is, but which is better is hard to tell (and dependent on the actually
implementation and compiler). Generally, if the performance difference isn't
significant, use a more flexible design (which you can easily make
optimizations later on).

Also - is the above a bad practice?? Experienced coders at my firm
say it's not a good design to use function pointers.
What's wrong with function pointers? Be specific. I can easily see some
pitfalls with switch/case approach: it reduces modularity, it is sticky
(cannot isolate things), its rigid (cannot be easily extended), and it is
static (cannot be dynamically modified).

If you want more flexibility, try boost::function template (safer, more
flexible, easier to use)

thanks a lot for your comments & help

Why not use std::map<CaseType, boost::function<void (void)> >, assuming you
are using CaseType as case type and void(void) as handler function.
Otherwise using hashmaps is also a good idea.

Ben
Sep 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Richard A. DeVenezia | last post by:
At line this.timerId = setInterval (function(){this.step()}, 100) I get error dialog Error:Object doesn't support this property or method. If I change it to this.timerId = setInterval...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
29
by: shmartonak | last post by:
For maximum portability what should the type of an array index be? Can any integer type be used safely? Or should I only use an unsigned type? Or what? If I'm using pointers to access array...
27
by: Marlene Stebbins | last post by:
I am experimenting with function pointers. Unfortunately, my C book has nothing on function pointers as function parameters. I want to pass a pointer to ff() to f() with the result that f() prints...
2
by: newhand | last post by:
If somehow a bunch of function names in string can be passed into an executible, is it possible that for each name call, it will trigger the corresponding function of that name? Of course, this can...
6
by: daveyand | last post by:
Hey Guys, I've stumped. I created a function that does various things to select boxes. Namely Get All selected indexes, populate array with these values
33
by: Zytan | last post by:
I want to make a zero element array. I know that Nothing is not the same as a zero element array, since I can't get the length of, or iterate through, an array = Nothing. I could make a zero...
26
by: aruna.mysore | last post by:
Hi all, I have a specific problem passing a function pointer array as a parameter to a function. I am trying to use a function which takes a function pointer array as an argument. I am too sure...
5
by: ctj951 | last post by:
I have a very specific question about a language issue that I was hoping to get an answer to. If you allocate a structure that contains an array as a local variable inside a function and return...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
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...
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.