Thread: casting to void instead of using free?

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    269

    casting to void instead of using free?

    Code:
    data_free(void *event_data, void *client_data)
    {
        (void) client_data;
        free(event_data);
    }
    What's the purpose of (void)client_data ?? It's already being passed in as a void pointer....

  2. #2
    Registered User
    Join Date
    Mar 2008
    Location
    Coimbra, Portugal
    Posts
    85
    I'd say that it is used to avoid a compiler warning about client_data not being used. However, there are much simpler and beautiful approaches to that...

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    269
    Quote Originally Posted by Jorl17 View Post
    I'd say that it is used to avoid a compiler warning about client_data not being used. However, there are much simpler and beautiful approaches to that...
    I see. is there any reason why you'd pass it in if it's not going to be used??

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Quote Originally Posted by dayalsoap View Post
    I see. is there any reason why you'd pass it in if it's not going to be used??
    It's possible it is implementing the prototype of a function pointer, but in this implementation, only one of the parameters is used.
    bit∙hub [bit-huhb] n. A source and destination for information.

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Jorl17 View Post
    I'd say that it is used to avoid a compiler warning about client_data not being used. However, there are much simpler and beautiful approaches to that...
    Such as what? Casting to void is pretty much the standard way of doing it.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by brewbuck View Post
    Such as what? Casting to void is pretty much the standard way of doing it.
    I've barely ever seen it done that way. 99% of what I see is simply the commenting out of the parameter name. I.e:
    Code:
    data_free(void *event_data, void * /*client_data*/)
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >99% of what I see is simply the commenting out of the parameter name.
    Which only works in C++. In C it's a syntax error to have an unnamed parameter in a function definition.
    My best code is written with the delete key.

  8. #8
    Registered User
    Join Date
    Mar 2008
    Location
    Coimbra, Portugal
    Posts
    85
    Quote Originally Posted by brewbuck View Post
    Such as what? Casting to void is pretty much the standard way of doing it.
    I just let the argument name out. In the case I want to keep it in anyway, I define a macro:

    Code:
    #define UNUSED(x)
    Then I would do:

    Code:
    data_free(void *event_data, void * UNUSED(client_data))
    I think that's also what Qt4 does.


    However:

    >99% of what I see is simply the commenting out of the parameter name.
    Which only works in C++. In C it's a syntax error to have an unnamed parameter in a function definition.
    I see that, I didn't know it! So it seems that it really is one of the most standard ways of doing it. Other than the classical x = x;

    Maybe it could be cleaner with a macro...maybe not!

    Also, *without* optimization, doesn't the void method produce a nop?

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Also, *without* optimization, doesn't the void method produce a nop?
    It should produce a no-op regardless (though an exceptionally poor compiler may not do so). The cast simply silences a potential warning.
    My best code is written with the delete key.

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Prelude View Post
    >99% of what I see is simply the commenting out of the parameter name.
    Which only works in C++. In C it's a syntax error to have an unnamed parameter in a function definition.
    Well I guess that speaks volumes about which language I use most
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issues with 16-bit dos code
    By JungleJesus in forum C Programming
    Replies: 10
    Last Post: 05-06-2010, 01:44 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Replies: 3
    Last Post: 05-13-2007, 08:55 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. Just one Question?
    By Irish-Slasher in forum C++ Programming
    Replies: 6
    Last Post: 02-12-2002, 10:19 AM