C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 23rd, 2004, 03:51 PM
c0ldshadow c0ldshadow is offline
register char *c0ldMember;
Dev Shed Novice (500 - 999 posts)
 
Join Date: Nov 2003
Posts: 510 c0ldshadow User rank is Private First Class (20 - 50 Reputation Level)c0ldshadow User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 3 Days 15 m 46 sec
Reputation Power: 10
what does it mean to zero out a struct?

just a random question that i figured someone might know the answer to: what does it mean to zero out a struct? examples would be great. just any help would be great.

peace, --ave

Reply With Quote
  #2  
Old July 23rd, 2004, 04:12 PM
infamous41md's Avatar
infamous41md infamous41md is offline
not a fan of fascism (n00b)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Feb 2003
Location: ct
Posts: 2,756 infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 2 Days 11 h 4 m 29 sec
Reputation Power: 94
Code:
struct foo bar;

memset(&bar, 0, sizeof(bar));

Reply With Quote
  #3  
Old July 23rd, 2004, 08:56 PM
gt3_dk's Avatar
gt3_dk gt3_dk is offline
!Ruff Ryder!
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jun 2004
Location: Québec, Canada... Represent!
Posts: 689 gt3_dk User rank is Corporal (100 - 500 Reputation Level)gt3_dk User rank is Corporal (100 - 500 Reputation Level)gt3_dk User rank is Corporal (100 - 500 Reputation Level)gt3_dk User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 3 Days 7 h 10 m 12 sec
Reputation Power: 12
Send a message via Yahoo to gt3_dk
Well to zero it out is to make it all NULL or zero... what infamous41md did is that he just "set up" a location in memory for it which was all cleared out. This is good because it's shorter than typing bar.x = 0; bar.y = 0; bar.... one after another... (and it also takes less time to execute...(right?)!) and also so that you don't have wrong uninitilized variables in your programs!

Reply With Quote
  #4  
Old July 23rd, 2004, 09:37 PM
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Apr 2004
Posts: 1,676 Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Month 3 Days 8 h 23 m 46 sec
Reputation Power: 132
An all-bits-zero representation may not be correct for a floating point zero or a null pointer constant.

You may want to also consider something like either of these.
Code:
struct type object = {0}; /* zero out struct at initialization */
static const struct type zero = {0};
/* ... */
object = zero; /* zero out struct by assignment */

Reply With Quote
  #5  
Old July 24th, 2004, 02:05 AM
Spectre5 Spectre5 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 337 Spectre5 User rank is Private First Class (20 - 50 Reputation Level)Spectre5 User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Day 9 h 34 m 56 sec
Reputation Power: 9
Why do you zero out a structure?? I have used them and I have never zeroed them out.....but my programs work fine...

what does zeroing out a structure do?

Reply With Quote
  #6  
Old July 24th, 2004, 06:02 AM
DaWei_M's Avatar
DaWei_M DaWei_M is offline
Lord of Dorkness
Dev Shed God 8th Plane (8500 - 8999 posts)
 
Join Date: Jan 2004
Location: Central New York. Texan via Arizona, out of his element!
Posts: 8,515 DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level) 
Time spent in forums: 4 Weeks 18 h 59 m 31 sec
Warnings Level: 20
Number of bans: 3
Reputation Power: 3268
Zeroing a structure is just a method of initializing it to known (possibly inappropriate, as User Name points out) values. It isn't necessary to overtly initialize all things, so long as they are initialized (or their value otherwise correctly set) prior to use. Since we're human, however, and tend to forget things, judicious initialization is a good idea. Initializing a pointer to NULL may not give you a usable value, but it DOES allow you to test that it's not usable, which is a gain over some indeterminate value it might have after creation.
__________________
Functionality rules and clarity matters; if you can work a little elegance in there, you're stylin'.
If you can't spell "u", "ur", and "ne1", why would I hire you? 300 baud modem? Forget I mentioned it.
DaWei on Pointers Politically Incorrect.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > what does it mean to zero out a struct?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap