Does C have a NULL constant function?

T

Thomas L.

Hi all,
Does a "NULL constant function" exist in C? I mean:
I am searching for a C language constant which would be something like
a function but which would be translated by a compiler into "nothing":
no argument push in the stack pointer, no execution of the function
instructions, no cleaning up, nothing, nada, rien, vide...

I could then initialize a function pointer f to that constant function
and ask
(*f)(any_argument_list)
and the compiler would translate it and write in the text section:

*NOTHING*

I apologize if it seems "pathetically useless" but actually I would
find it the right tool to perform, for example,
(*MyVector->methods->NormalizeVector)(MyVector->info) if I know
MyVector is unit-length (so that it would not be modified by the
NormalizeVector function).
To perform the normalization is useless, time consuming and I am
exposed to rounding errors.

Any input appreciated
Thomas
 
D

Dan Pop

In said:
Does a "NULL constant function" exist in C? I mean:
I am searching for a C language constant which would be something like
a function but which would be translated by a compiler into "nothing":
no argument push in the stack pointer, no execution of the function
instructions, no cleaning up, nothing, nada, rien, vide...

I could then initialize a function pointer f to that constant function
and ask
(*f)(any_argument_list)
and the compiler would translate it and write in the text section:

*NOTHING*

There is no such thing and you can't even use something like

void null(void) {}

for this purpose, because calling this function with a non-empty argument
list invokes undefined behaviour.

Dan
 
B

Ben Pfaff

Does a "NULL constant function" exist in C? I mean:

There's the null pointer constant. It's expressed as "0".
I am searching for a C language constant which would be something like
a function but which would be translated by a compiler into "nothing":
no argument push in the stack pointer, no execution of the function
instructions, no cleaning up, nothing, nada, rien, vide...

I could then initialize a function pointer f to that constant function
and ask
(*f)(any_argument_list)
and the compiler would translate it and write in the text section:

*NOTHING*

That's not what a null function pointer does. Instead,
invoking it yields undefined behavior. But you could get similar
behavior by testing whether the pointer is non-null:
if (f)
f(args);
 
B

Ben Pfaff

Dave said:
void null(...) { }

Is that valid C?

No, varargs functions must have at least one fixed argument.
Otherwise what would you specify as the second argument to
va_start()?

Besides that, varargs functions are not compatible with
non-varargs functions.
 
T

Thomas L.

Ben Pfaff said:
That's not what a null function pointer does. Instead,
invoking it yields undefined behavior. But you could get similar
behavior by testing whether the pointer is non-null:
if (f)
f(args);

Yes, sure.
But it's perhaps even less costy to point my pointer function with a
void *f(argument_list){}? (Perhaps some compilers can optimize this to
*noting*) There is compile-time type-checking, at run-time the passing
of argument on the stack is there, and I find it more "clean".
 

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,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top