Component 10
Component 10

Reputation: 10497

Use of (apparently) empty C function

Can anyone comment on the point of the following function, which appears to do not very much:

// Returns stored values
int getDetails(const int param1[],
               int* param2,
               int* param3,
               int* param4)
{
    (void)param1;
    (void)param2;
    (void)param3;
    (void)param4;
    return 0;
}

The comment is actually there with the code. I'm thinking it must be some kind of odd stub but it is being called and I'm racking my brains to try to imagine what I'm missing.

My best hunch so far is that the function has been deprecated but not removed and the (void)param is to avoid compiler warnings about unused variables.

Upvotes: 2

Views: 3991

Answers (5)

simonc
simonc

Reputation: 42185

Statements like (void)param1; are typically used to suppress warnings about unused function parameters. (As an aside, in C++ you could also comment out or remove the parameter names.)

You're correct that the function does nothing. If other code doesn't create a pointer to it, you could safely remove it.

Upvotes: 5

Alexey Frunze
Alexey Frunze

Reputation: 62086

It's an empty function. Casts to void suppress warnings about unused parameters.

Such functions are often used when a function must be called unconditionally or a valid function pointer must be provided, but really the function has nothing to do.

I have a few such functions in my compiler's code generator. There are two code generators actually, one for x86 and the other for MIPS. I compile and link one or the other, never both at the same time.

The code generators are different internally but have the same external API. So, some functions specific to one CPU have some work to do while the same functions for the other have nothing to do. Logically, some of them are empty since there's nothing to do.

Upvotes: 4

user2247801
user2247801

Reputation: 145

Some Compilers shows error/warning when you are not using the arguments passed to it , to avoid that mention that like it in your code . If the function is not called any where or not assigned to any function pointers , you can remove it as it is not doing anything specific

Upvotes: 1

LoztInSpace
LoztInSpace

Reputation: 5697

My guess (opinion - sorry!) that it could be a stub as you say. If you have a function that takes one or more function pointers to achieve something and it does not allow for a NULL (don't bother with this) then you have to provide something for it to call.

The casts are probably to avoid the "unused parameter" warning.

Upvotes: 2

unwind
unwind

Reputation: 399949

You're right, it has no point.

All it does is explicitly ignore the arguments by evaluating them and casting the result to (void), and return 0.

Is the return value being used in the context of the call? The best approach is of course to remove the call and replace it with a 0 if the return value is being used, and test the program.

Upvotes: 1

Related Questions