Question

void table_no_op()
{
// this is for function table elements that do nothing,
// fills space between states, use less of them
    return;
}

I am currently using this to define a "zero" in a function pointer table, where the input index is supposed to do nothing. Is it okay or something glaringly wrong?

Was it helpful?

Solution

While there's nothing wrong with the no-op per se (in general, this is called Null Object Pattern), I would be worried about the function declaration - i.e. does every function in the table take 0 arguments and return void?

A counterexample would be OpenGL where you often retrieve a pointer to function and cast it to the desired type yourself - but casting a void->void pointer to something else, e.g. (int, int)->int would be undefined behavior and likely cause crash (or uninitialized return value, or else).

So, if the functions in the table are homogeneous - go for it. If not - better do something else.

EDIT: You can only do 2 things with a function pointer - cast it to a different function pointer; and call, but only with the original type.

See http://blog.frama-c.com/index.php?post/2013/08/24/Function-pointers-in-C for details. Raymond Chen has another example here - http://blogs.msdn.com/b/oldnewthing/archive/2011/05/06/10161590.aspx

EDIT2: However, you may make a number of no_ops (noop_IntInt_Int, noop_IntDouble_Double and so on... then if you match the types every time, that might work)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top