Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: Various compiler questions

Author: Gareth McCaughan

Date: 12:03:12 01/14/02

Go up one level in this thread


On January 14, 2002 at 12:39:33, Bo Persson wrote:

>Your second example will probably be implemented exactly as would:
>
>if (var == 297)
>  ...
>else if (var == 0)
>  ...
>else if (var == 9000)
>  ...

If you have enough cases, the compiler might put them into
a more balanced tree instead: compare against some value in
the middle of the range, then some value in the middle of
that range, etc. At some point in the recursion it will
probably be well advised to fall back to code like yours
above.

> BTW, I have almost *never* found good use for a function pointer table.
> Care to give me some hints?  :-)

You use one every time you call a virtual member function in C++.
If you don't use C++ but want to do OOish things, then it can be
useful to use the technique directly.

If you build a virtual machine (e.g., in a language implementation)
then you may have an array of function pointers at its core.

Imagine you're writing code to parse the command-line options for
a program. And imagine that lots of the options have the effect of
setting some variable to a particular value. Then you might have
a table of structs each of which contains

  - an option name
  - a pointer to a function whose job is to interpret command-line args
    that form parameters to this one and store the results
    somewhere sensible
  - some further data that will be passed to the function (which might
    contain, e.g., the address of the variable it's setting).

--
g



This page took 0 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.