What are these statements for?

This is a discussion on What are these statements for? within the C Programming forums, part of the General Programming Boards category; Hi Code: static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) { (void)ud; (void)osize; if (nsize == 0) ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    519

    What are these statements for?

    Hi

    Code:
    static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {
      (void)ud;
      (void)osize;
      if (nsize == 0) {
        free(ptr);
        return NULL;
      }
      else
        return realloc(ptr, nsize);
    }
    in the function above: Could anybody tell me what the red statements are for? They should have absolutely no effect to the program logic.
    The only thing I can think of is that the compiler is used to check something about the condition of the passed arguments.
    But why casting a void* to void? I didn't even know that this is legal in C/C++.

    Thank you!

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    5,636
    to hide the warning
    warning C4100: 'ud' : unreferenced formal parameter
    If I have eight hours for cutting wood, I spend six sharpening my axe.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    Ah thanks.

    Code:
    static void *l_alloc (void */*ud*/, void *ptr, size_t osize, size_t nsize)
    would be a bit nicer in my eyes.

    But: Why can I cast to void but not declare something as void, e.g.
    Code:
    void a; //error
    void* b;
    (void)b; //legal

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    5,636
    Quote Originally Posted by pheres View Post
    Ah thanks.

    Code:
    static void *l_alloc (void */*ud*/, void *ptr, size_t osize, size_t nsize)
    would be a bit nicer in my eyes.
    we are talking C here?

    error C2055: expected formal parameter list, not a type list

    Why can I cast to void but not declare something as void, e.g.
    void is not a type

    void* is type
    If I have eight hours for cutting wood, I spend six sharpening my axe.

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    I wasn't aware of the fact that one can cast to anything else than a type.
    For whats reason is that possible or what could someone possibly do with the result?

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I think that is just a way to signal that the line should be a no-op (meaning something like: ignore the result). Without (void) the compiler would warn that the statement has no effect. Now it sees that it is not supposed to have an effect.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 03:09 AM
  2. newbie question - if statements without conditions
    By c_h in forum C++ Programming
    Replies: 2
    Last Post: 07-18-2008, 10:42 AM
  3. Explanation of switch statements
    By ammochck21 in forum C++ Programming
    Replies: 6
    Last Post: 11-04-2006, 01:59 PM
  4. Efficiency of case statements
    By Yasir_Malik in forum C Programming
    Replies: 26
    Last Post: 05-23-2006, 11:36 AM
  5. Need help with "if" statements
    By Harryt123 in forum C Programming
    Replies: 22
    Last Post: 05-14-2006, 08:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21