sockaddr_in

This is a discussion on sockaddr_in within the Networking/Device Communication forums, part of the General Programming Boards category; Is it ok to zero out "sockaddr_in" like this: Code: sockaddr_in clientService = {0}; Instead of the usual: Code: sockaddr_in ...

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

    sockaddr_in

    Is it ok to zero out "sockaddr_in" like this:

    Code:
    sockaddr_in clientService = {0};
    Instead of the usual:

    Code:
    sockaddr_in clientService;
    memset( &clientService, 0, sizeof clientService );
    Compiler MSVC++ 2010 with Code::Blocks.

  2. #2
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    yep, perfectly ok. It may not necessarilly have the effect you intend though. sockaddr_in is a structure, as such you need to zero out every member variable in it.
    Last edited by abachler; 04-14-2009 at 04:08 AM.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    760
    Thanks Abachler!

    I thought this technique works the same for both arrays and structures.

    In an array it zeros out every element.
    Compiler MSVC++ 2010 with Code::Blocks.

  4. #4
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    It may work for your particular complilers implimentation, but afaik it is undefined behavior and may not be portable to other compilers.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Quote Originally Posted by abachler View Post
    It may work for your particular complilers implimentation, but afaik it is undefined behavior and may not be portable to other compilers.
    I don't believe that is undefined behavior. Is should initialize the members to zero.

Popular pages Recent additions subscribe to a feed

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