variable number of arguments + function pointers

A

Alex

Is there any problem with sending function pointers through in a
variable argument list? I have a function like the following:

typedef (*ptr2FuncType1)( int );
typedef (*ptr2FuncType2)( double );

void SomeClass::DoSomething( unsigned int num_something, unsigned long
num_something_else, ptr2FuncType1 handler_one, ... )

The functions I am passing in are in a different cpp file, but they
are not memeber functions of any class.

Where the ... represents the function pointers which I am sending to
the function. Inside that function I initialize a member array that
contains those pointers to functions. So I use va_start( arglist,
handler_one ) to start the arg list.

Then to get to each function pointer I use
m_pFuncArray = va_arg( arglist, ptr2FunctionType2 );

etc....

When I try and call the functions later in another class function, I
get a program crash from memory error. The thing I am thinking is that
the program needs to code segment as well as the address of the
function to be able to call it. Is there anything special I need to do
with variable arguments to get functions pointers to work correctly?
Thanks!

Alex Earl
 
R

Ron Natalie

Alex said:
Then to get to each function pointer I use
m_pFuncArray = va_arg( arglist, ptr2FunctionType2 );


Provided that the argument is really of type ptr2FunctionType2, this should be ok.
Are you sure you haven't screwed up the types. There is NO checking provided
by va_arg. Also make sure you have extracted the prior args in the list correctly
as once you screw one up the ohters can be hosed.
When I try and call the functions later in another class function, I
get a program crash from memory error. The thing I am thinking is that
the program needs to code segment as well as the address of the
function to be able to call it. Is there anything special I need to do
with variable arguments to get functions pointers to work correctly?

The compier is supposed to take all that into account when encoding the
pointer.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top