Find the answer to your Linux question:
Results 1 to 7 of 7
I found this snippet of code and I'm having a hard time understanding why its preceded by (void)... Code: (void)signal(SIGINT, SIG_DFL); When I remove the (void) the functions appears to ...
  1. #1
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714

    [SOLVED] signals in C

    I found this snippet of code and I'm having a hard time understanding why its preceded by (void)...

    Code:
               (void)signal(SIGINT, SIG_DFL);
    When I remove the (void) the functions appears to work properly...is there any reason for the the (void)?...Gerard4143

    Code:
                signal(SIGINT, SIG_DFL);

  2. #2
    Linux Engineer b2bwild's Avatar
    Join Date
    Jul 2008
    Location
    Behind You!
    Posts
    1,108
    Never make any misteaks.

    Read my Blog at --> Penguin Inside Subscribe Feed

  3. #3
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    Interesting article but it doesn't answer the question...Why would you have code

    Code:
    (void)signal(SIGINT, SIG_DFL);
    Since the (void) doesn't server any purpose that I can see and the function call
    Code:
    signal(SIGINT, SIG_DFL);
    produces the same result...Gerard4143

    Ooops there it is at the end of the article...a throw back from earlier times that explains the casting to (void)...Thanks

  4. #4
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    a throw back from earlier times
    It's a good answer, and almost complete.

    Two comments:
    1. Some more modern products can also check whether you look at the value returned from a function call. If you call a particular function several times in your code, and check the result most of the time, (void) can be used to override the checking of the one call where (you hope!) it doesn't matter.
    2. Although it's difficult to accidentally get an error from signal(), as a matter of form it's a good idea to check the return value anyway. You may have coded correctly, but if someone comes along later and modifies the code incorrectly, wouldn't you want him to have some notification that this had happened? (Of course, this is a religious issue.)
    --
    Bill

    Old age and treachery will overcome youth and skill.

  5. #5
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    Thank-you for the replies. I think in this case its a throw back because:

    1. the function result is not compared to anything and
    2. the function result is not saved.

    Again thank-you for the replies...Grerard4143

  6. #6
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    1. the function result is not compared to anything and
    2. the function result is not saved.
    True, but that doesn't necessarily imply use of an ancient C compiler. I fear that I didn't make myself clear previously. Allow me to muddy the waters a little more. :)

    Modern compilers usually issue neither error indications nor warnings for statements like these:
    Code:
    signal(SIGINT, SIG_DFL);
    printf("Hello, world!\n");
    Someone who wishes to write bulletproof code, though, will be interested in flagging many of these. He might even be interested in checking each printf() use for a negative returned value, which would indicate an error.

    To enforce this, he might find and use a product similar to the old flex (no relation to the Adobe product), which may still be available on some commercial UNIX systems. Its purpose was to be hypercritical of C programs, looking for unusual (though legal) code which could contain bugs.

    Or he can use a commercial product, like the one I linked to previously. I have used it and recommend it. ("They" are not paying me, and probably don't even know that I'm talking about their product.)

    Someone can use such a product, instructing it to warn about any calling of a function without using the returned value. This is useful when writing bulletproof code, because it forces the user to confront each place where his code doesn't check for an error indication upon return from a function call.

    It's perfectly reasonable for the user to do this, but still not be interested in error checking at certain points in his code. At such points, he will call functions as in this example:
    Code:
    (void)signal(SIGINT, SIG_DFL);
    Such a statement doesn't do anything different, but it does force the code checking software to avoid worrying about what is done with the returned value from the function call.

    Ok, then. So the above example will hint at the use of either an old C compiler, or a source code checking program.

    I hope this is a teensy bit clearer. :)
    --
    Bill

    Old age and treachery will overcome youth and skill.

  7. #7
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    Now i see what your driving at Bill...your responses are always a learning experience...Thanks again Gerard4143

    Just to be sure...your saying a programmer would use the void cast (as in my example) to explicitly state that the return value should be ignored...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...