What is there a need to zero out sockaddr_in structs?

This is a discussion on What is there a need to zero out sockaddr_in structs? within the Networking/Device Communication forums, part of the General Programming Boards category; Hi, I happened to read up on some nix sockets and I'm kinda hooked. The sample scripts often mentioning about ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    41

    What is there a need to zero out sockaddr_in structs?

    Hi,

    I happened to read up on some nix sockets and I'm kinda hooked.

    The sample scripts often mentioning about zero-ing up the structs of sockaddr_in.

    Code:
    memset(&(my_addr.sin_zero), \0, 8); // zero the rest of the struct
    Are there any reasons that remaining portions of structs be "Zeroed"?

    Thanks and Regards,
    Steven.
    If I'm able to turn back time, I would learn C as my first language.

  2. #2
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    The same reason for zeroing any struct, when you pass it to some black-box function, that function may work on some of the items if they are not null, this may break things.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    It's good programming practice to zero things even if you think the function dosen't use the extra structure members.

  4. #4
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    269
    hi

    sockaddr_in is written for a convenience use of internet sockets over sockaddr. In order to make the sizes of both structs equal you need 8 more bytes which are zeroed by memset().

  5. #5
    Logic Junkie
    Join Date
    Nov 2005
    Posts
    31
    Quote Originally Posted by rockytriton
    The same reason for zeroing any struct, when you pass it to some black-box function, that function may work on some of the items if they are not null, this may break things.
    How can it happen that they are not null when you have just declared a new struct? As in,
    Code:
     struct foo bar = malloc(sizeof(struct foo));

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,764
    malloc doesn't say anything about the content of the memory returned - it's uninitialised.
    Debug versions of malloc in particular often fill the memory with a pattern to help detect common malloc related programming errors.

    Use calloc if you really want allocated memory full of zero bytes, but beware of the type of memory you're actually allocating.
    http://www.eskimo.com/~scs/C-faq/q7.31.html

  7. #7
    Logic Junkie
    Join Date
    Nov 2005
    Posts
    31
    Aha - thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating array of structs
    By knirirr in forum C++ Programming
    Replies: 12
    Last Post: 06-18-2008, 09:30 AM
  2. Multidimentional structs + memcpy() == FAIL
    By Viper187 in forum C Programming
    Replies: 8
    Last Post: 06-18-2008, 03:46 AM
  3. packed structs
    By moi in forum C Programming
    Replies: 4
    Last Post: 08-20-2002, 02:46 PM
  4. ArrayLists + Inner Structs
    By ginoitalo in forum C# Programming
    Replies: 5
    Last Post: 05-09-2002, 06:09 AM
  5. Searching structs...
    By Sebastiani in forum C Programming
    Replies: 1
    Last Post: 08-25-2001, 01:38 PM

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