Thread: sockaddr_in

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

    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++ 2013 with Code::Blocks.

  2. #2
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    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 05:08 AM.

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

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

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

  4. #4
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    It may work for your particular complilers implimentation, but afaik it is undefined behavior and may not be portable to other compilers.

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,267
    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
Website Security Test