gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

C/C++ > Used Unused Parameter - No Warning

#122820 - sgeos - Thu Mar 22, 2007 5:22 pm

If unused parameters are used, no warnings are issued. There are no adverse effects, as far as I can see. Does anybody know if adverse effects could ever occur? (On a different compiler with a different definition of UNUSED, perhaps?)
Code:
#include <stdio.h>
#define UNUSED __attribute__((unused))

unsigned int f(UNUSED unsigned int pU, UNUSED signed int pS)
{
        return pU + pS;
}

int main(void)
{
        unsigned int    a =  100;
        signed int      b = -1;
        printf("> %d\n", f(a, b));
        return f(a, b);
}


Code:
USER@Stardust ~/c/test
$ gcc -O0 -Wall -Wextra -ansi -pedantic -Werror -Wconversion -o test test.c

USER@Stardust ~/c/test
$ ./test.exe
> 99

USER@Stardust ~/c/test
$ gcc -O3 -Wall -Wextra -ansi -pedantic -Werror -Wconversion -o test test.c

USER@Stardust ~/c/test
$ ./test.exe
> 99

USER@Stardust ~/c/test
$

-Brendan

#122826 - kusma - Thu Mar 22, 2007 6:14 pm

I'm pretty sure that __attribute__((unused)) only silence compiler warnings.
Another way to silence them, btw is like this:
Code:

int test(int a, int b)
{
   a = a;
   b = b;
   return 1995;
}

#122859 - keldon - Fri Mar 23, 2007 12:03 am

A future compiler may identify that as an unused parameter as it is never set to anything that is not itself.

#122888 - sgeos - Fri Mar 23, 2007 6:38 am

keldon wrote:
A future compiler may identify that as an unused parameter as it is never set to anything that is not itself.

If an UNUSED macro is defined and the code is ported, the macro may have to be redefined. In theory I could see problems with a different compiler. Then again, that compiler might issue a warning. To the extent that anything can be called with a function pointer the compiler can't do much. I just wish it issue a "pay more attention" warning. =)

-Brendan

#122903 - kusma - Fri Mar 23, 2007 12:19 pm

keldon wrote:
A future compiler may identify that as an unused parameter as it is never set to anything that is not itself.

Why would it? The compiler is perfectly capable of seeing what parameters are used and what are not in the first place - this is just a way of shutting the compiler up about warnings on unused parameters. Yes, it would be best if the compiler issued a warning if the unused parameter was actually used again, but if someone ever wrote a compiler that actually _broke_ on such cases, I wouldn't trust it for anything at all.

#122924 - keldon - Fri Mar 23, 2007 3:28 pm

Code:
public class UnusedVariables {
   public static void main ( String argv[] ) {
      int a;
      a = a;
   }
   
   public static int foo1(int a ){
      a=a;
      return a;
   }
}

Try to compile that code in Java and see what errors it gives.

#122925 - kusma - Fri Mar 23, 2007 4:22 pm

keldon wrote:
Try to compile that code in Java and see what errors it gives.

I think I misread your initial post, sorry about that ;)

#122929 - poslundc - Fri Mar 23, 2007 5:49 pm

Part of the problem with the whole unused-parameter-warning thing is that it loses its purpose one you introduce the concept of polymorphism and virtual functions.

When you have a function that exists purely on its own? Yeah, it doesn't make sense for it (at least in final implementation) to take a parameter it doesn't use. But in the OOP paradigm, where there's a compiler-enhanced separation of interface and implementation, it makes the warning a whole lot less meaningful.

Dan.

#122934 - sgeos - Fri Mar 23, 2007 7:26 pm

poslundc wrote:
where there's a compiler-enhanced separation of interface and implementation, it makes the warning a whole lot less meaningful.

This is my function pointer argument. "This function doesn't use the first parameter. It is an unary operation."

-Brendan