struct zero out

This is a discussion on struct zero out within the C Programming forums, part of the General Programming Boards category; I see it in codes that once they zerod out a struct they set a few elements to NULL. Is ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    796

    struct zero out

    I see it in codes that once they zerod out a struct they set a few elements to NULL.

    Is it really necessary or is it redundant?

    Code:
    ZeroMemory(&ofn, sizeof(ofn));
    ofn.lStructSize = sizeof(ofn);
    ofn.hwndOwner = NULL;
    Compiler MSVC++ 2013 with Code::Blocks.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Some tests become quite simple:

    Code:
    if(somePointer)
      //do something constructive if the pointer has been
      //given a good address.
    else
      //it's still a NULL value, so
      //give the pointer a good address
    It will NOT help the program keep running, even though it has poor logic.

  3. #3
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    984
    It's necessary for maximally portable code. There's no guarantee that null pointers are all bits zero. The same goes for floating point: 0.0 isn't necessary all bits zero. C99 (well, one of its technical corrigenda) does require that integers with all bits zero be set to zero. The implication is that C90 didn't require this, but this is probably one of those assumptions that you can safely assume will hold even though it wasn't required.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    796
    Thank you cas and Adak i understand it better.
    Compiler MSVC++ 2013 with Code::Blocks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. casting struct to struct
    By klmdb in forum C Programming
    Replies: 6
    Last Post: 08-14-2010, 02:29 PM
  2. Help me please.
    By yann in forum C Programming
    Replies: 15
    Last Post: 09-29-2009, 09:04 PM
  3. help with structs and malloc!
    By coni in forum C Programming
    Replies: 20
    Last Post: 09-14-2009, 05:38 PM
  4. Replies: 1
    Last Post: 12-03-2008, 02:10 AM
  5. Replies: 10
    Last Post: 05-18-2006, 11:23 PM

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