Thread: struct zero out

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

    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;
    Using Windows 10 with Code Blocks and MingW.

  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
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    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
    932
    Thank you cas and Adak i understand it better.
    Using Windows 10 with Code Blocks and MingW.

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, 03:10 AM
  5. Replies: 10
    Last Post: 05-18-2006, 11:23 PM