8

I need to use pthreat but I dont need to pass any argument to the function. Therefore, I pass NULL to the function on pthread_create. I have 7 pthreads, so gcc compiler warns me that I have 7 unsued parameters. How can I define these 7 parameters as unused in C programming? If I do not define these parameters as unused, would it cause any problem? Thank you in advance for the responses.

void *timer1_function(void * parameter1){
//<statement>
}

int main(int argc,char *argv[]){
  int thread_check1;
  pthread_t timer1;
  thread_check1 = pthread_create( &timer1, NULL, timer1_function,  NULL);
    if(thread_check1 !=0){
        perror("thread creation failed");
        exit(EXIT_FAILURE);
    }
while(1){}
return 0;
}
Kijewski
  • 25,517
  • 12
  • 101
  • 143
johan
  • 1,943
  • 10
  • 31
  • 43
  • If they're unused, it implies that no meaningful operations are done to those variables, and (for the most part) they're safe to get rid of. It's a warning, not an error, so it *can* be ignored. It's usually not a good idea *to* ignore it, but you *can*. – Makoto Apr 30 '12 at 21:49
  • @hmjd - C++ allows it, not C. – MByD Apr 30 '12 at 21:55
  • 2
    http://stackoverflow.com/q/7090998/168175 – Flexo Apr 30 '12 at 21:57
  • possible duplicate of [Universally compiler independant way of implementing an UNUSED macro in C/C++](http://stackoverflow.com/questions/4851075/universally-compiler-independant-way-of-implementing-an-unused-macro-in-c-c) – Michael Burr Apr 30 '12 at 22:35
  • 1
    possible duplicate of [unused parameter warnings in C code](http://stackoverflow.com/questions/3599160/unused-parameter-warnings-in-c-code) – Lesmana Jan 14 '13 at 19:11

5 Answers5

19

You can cast the parameter to void like this:

void *timer1_function(void * parameter1) {
  (void) parameter1; // Suppress the warning.
  // <statement>
}
Adam Liss
  • 47,594
  • 12
  • 108
  • 150
  • 3
    http://stackoverflow.com/a/4851173/168175 has an alternative form that works better for `volatile` apparently – Flexo Apr 30 '12 at 21:56
18

GCC has an "attributes" facility that can be used to mark unused parameters. Use

void *timer1_function(__attribute__((unused))void *parameter1)
Kyle Jones
  • 5,492
  • 1
  • 21
  • 30
2

Two commonly used techniques:

1) Omit the name of the unused parameter:

void *timer1_function(void *) { ... }

2) Comment out the parameter name:

void *timer1_function(void * /*parameter1*/) { ... }

-- Chris

Chris Pearson
  • 162
  • 1
  • 3
1

By default, GCC does not produce this warning, not even with -Wall. I think the workaround shown in other question might be needed when you have no control over the environment, but if you do, just remove the flag (-Wunused-parameter).

MByD
  • 135,866
  • 28
  • 264
  • 277
  • +1 this is the best fix. This warning is fundamentally idiotic. Whenever the address of a function is taken, GCC should turn off "unused parameter" warnings for it, because whether they're used internally or not, they're used as part of the required interface for the function. Personally I would say the same thing should apply to all external functions too... – R.. GitHub STOP HELPING ICE Apr 30 '12 at 23:57
  • 3
    @R I caught a bug in my code a few days ago thanks to this warning. I was doing a refactor of some functions and typed 0 instead of the identifier for a bitmask that came in as a function parameter. Enabling -Wextra allowed me to immediately fix a subtle bug that had been introduced days ago. – Kyle Jones May 02 '12 at 17:34
  • 2
    -1: I regularly find bugs thanks to this warning being activated. – Étienne Jul 09 '14 at 09:50
  • 1
    For code that is many years old and modified... over time sometimes args are added and end up being unused later on, so this warning can be very useful, especially when it becomes a hassle to pass some argument to a function (lookup or if it has to be calculated), only to find its not even used. – ideasman42 Aug 20 '14 at 14:30
0

It is perfectly fine not using a parameter in a function body.

To avoid the compiler warning (if any in your implementation), you can do this:

void *timer1_function(void * parameter1)
{
    // no operation, will likely be optimized out by the compiler
    parameter1 = parameter1;  
}
ouah
  • 142,963
  • 15
  • 272
  • 331