C Board  

Go Back   C Board > General Programming Boards > Networking/Device Communication

Reply
 
LinkBack Thread Tools Display Modes
Old 04-14-2009, 03:51 AM   #1
Registered User
 
Join Date: Dec 2007
Location: France
Posts: 430
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 );
__________________
Using Code::Blocks,MingW with Windows.
Ducky is offline   Reply With Quote
Old 04-14-2009, 04:06 AM   #2
Malum in se
 
abachler's Avatar
 
Join Date: Apr 2007
Posts: 3,188
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.
__________________
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.

Last edited by abachler; 04-14-2009 at 04:08 AM.
abachler is offline   Reply With Quote
Old 04-14-2009, 04:30 AM   #3
Registered User
 
Join Date: Dec 2007
Location: France
Posts: 430
Thanks Abachler!

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

In an array it zeros out every element.
__________________
Using Code::Blocks,MingW with Windows.
Ducky is offline   Reply With Quote
Old 04-14-2009, 05:40 AM   #4
Malum in se
 
abachler's Avatar
 
Join Date: Apr 2007
Posts: 3,188
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.
abachler is offline   Reply With Quote
Old 04-14-2009, 02:42 PM   #5
Registered User
 
Join Date: Sep 2004
Location: California
Posts: 3,020
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.
bithub is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump


All times are GMT -6. The time now is 04:08 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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