[wplug] C Language Question

Chet Hosey chosey at nauticom.net
Thu Dec 8 08:45:19 EST 2005


Diana A. Clarion wrote:

>I've been looking at some code that involves signal handlers, and have
>encountered a construct I simply don't recognize.
>
>The context:  A handler for SIGINT
>
>void intr(int ignore)
>{
>	(void)ignore;
>	siglongjmp(toplevel, 1);
>}
>
>The construct in question is "(void)ignore;".  I just don't seem to be able to
>find a reference to it anywhere.  Is it a cast to some sort of 'void variable',
>to ensure that the variable 'ignore' is unusable, or is it something else?
>
>Many much thanks in advance,
>DAC
>
>  
>
I can't see how the statement has any effect. It might be a trick to get
a particular compiler to stop warning about an unused parameter, but I
don't believe GCC does anything to that effect (and I'm unsure of other
compilers).

Putting a variable or function name on a line by itself will cause GCC
to give a "statement has no effect" warning when compiling with -Wall.
Other compilers probably have a similar feature, since depending on
language background some might tend to omit parenthesis when calling a
function without parameters:

int doSomething();

int main() {
    doSomething;   // has no effect
    doSomething();   // does something
}

The cast to (void) prevents such a warning, since this doesn't look like
a mistyped function call to the compiler. However, since you're asking
for a conversion to void, no code is actually produced.

When using optimization, GCC will refrain from pushing the unused
parameter onto the stack before the function call (since it knows that
it won't be used). I'd thought that the cast to void would somehow
"count" as a reference (in case the siglongjump depends on the parameter
somehow), but it doesn't seem to:

#include <stdio.h>

void test(int ignore)
{
        (void) ignore;
        printf("test\n");
}

int main()
{
        test(1234);
        return 0;
}

chet at colo:~$ gcc -S ignoreparam.c -o plain.s
chet at colo:~$ gcc -O3 -S ignoreparam.c -o opt.s
chet at colo:~$ diff -u plain.s opt.s | tail -20
        subl    $8, %esp
        andl    $-16, %esp
-       movl    $0, %eax
-       addl    $15, %eax
-       addl    $15, %eax
-       shrl    $4, %eax
-       sall    $4, %eax
-       subl    %eax, %esp
-       subl    $12, %esp
-       pushl   $1234         # here is the parameter stack push
-       call    test
-       addl    $16, %esp
-       movl    $0, %eax
+       subl    $28, %esp   # stack frame is adjusted, but no push here
+       pushl   $.LC0
+       call    puts
+       xorl    %eax, %eax
        leave
        ret
        .size   main, .-main
chet at colo:~$

It's got to be something to prevent warnings, or even just to flag (for
the programmer) the fact that the parameter truly is unused. It
certainly wouldn't have a side-effect on the variable's contents, and
would not prevent further use later in the function.

By the way, is this code intended for use with multiple (or non-GCC)
compilers? I really feel like this is probably to prevent a warning
somewhere...

Chet


More information about the wplug mailing list