The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
what does it mean to zero out a struct?
Discuss what does it mean to zero out a struct? in the C Programming forum on Dev Shed. what does it mean to zero out a struct? C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

July 23rd, 2004, 03:51 PM
|
|
register char *c0ldMember;
|
|
Join Date: Nov 2003
Posts: 510

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
|

July 23rd, 2004, 04:12 PM
|
 |
not a fan of fascism (n00b)
|
|
Join Date: Feb 2003
Location: ct
|
|
Code:
struct foo bar;
memset(&bar, 0, sizeof(bar));
|

July 23rd, 2004, 08:56 PM
|
 |
!Ruff Ryder!
|
|
Join Date: Jun 2004
Location: Québec, Canada... Represent!
Posts: 689
  
Time spent in forums: 3 Days 7 h 10 m 12 sec
Reputation Power: 12
|
|
|
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!
|

July 23rd, 2004, 09:37 PM
|
 |
Contributing User
|
|
|
|
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 */
|

July 24th, 2004, 02:05 AM
|
|
Contributing User
|
|
Join Date: Jul 2004
Posts: 337

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?
|

July 24th, 2004, 06:02 AM
|
 |
Lord of Dorkness
|
|
Join Date: Jan 2004
Location: Central New York. Texan via Arizona, out of his element!
|
|
|
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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|