> -----Original Message-----
> From: Scott C. Gray [mailto:[EMAIL PROTECTED]]
> Sent: Thursday, August 24, 2000 11:52 PM
> To: Bernard Dautrevaux
> Cc: [EMAIL PROTECTED]
> Subject: RE: Tables of function pointers
> 
> 
> On Thu, 24 Aug 2000, Bernard Dautrevaux wrote:
> 
> > Probably the simplest (and more protable) cure would be to have an
> > 'init_function_table' function that initialize the function 
> array, like:
> > 
> >     void init_function_table() {
> >             static int initialized = 0;
> >             if (initialized) return;
> >             sg_func_table[0] = function_0;
> >             sg_func_table[1] = function_1;          
> >             sg_func_table[2] = function_2;
> >             sg_func_table[3] = function_3;          
> >             initialized = 1;
> >     }
> > 
> > and either call it at the very beginning of your main 
> program or, if it is a
> > general purpose library used in numerous programs and 
> performance is not too
> > critical, call it at the very beginning of call_function 
> (anyway any good C
> > compiler like gcc should be able to integrate it in-line, 
> especially if
> > declared static, so the cost is only a test and branch).
> 
> That will work, but unfortunately, the tables that I have have
> ~250 function pointers in them, so this method is a little bit
> awkward and relatively error prone...it is much easier to
> just edit the static array.
> 
> But, if I get really stuck I may just do this.

I *know* this is kludgy, but a little CPP trick may help:

void init_function_table() {
        static int initialized = 0;
        int i = 0;

        if (initialized) return;
#define Set(f)  sg_func_table[i++] = f;
        Set(function_0);
        Set(function_1);
        Set(function_2);
        Set(function_3);
#undef Set
        initialized = 1;
}


I'm sure you're able to write the global replace for your favorite editor to
change automatically the array initializer in the list of Set() calls... so
this is perhaps a little bit less readable, but not more error prone
although I will not argue it's elegant :-)

Regards,

        Bernard
        
--------------------------------------------
Bernard Dautrevaux
Microprocess Ingéniérie
97 bis, rue de Colombes
92400 COURBEVOIE
FRANCE
Tel:    +33 (0) 1 47 68 80 80
Fax:    +33 (0) 1 47 88 97 85
e-mail: [EMAIL PROTECTED]
                [EMAIL PROTECTED]
-------------------------------------------- 

Reply via email to