472,139 Members | 1,436 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,139 software developers and data experts.

Structure initialization and clearing

Hi All,

Quick question; what is the difference between initializing and
clearing a structure like this:

mystruct_t a = {0};
and initializing and clearing it like this:

mystruct_t a;

memset(a, 0, sizeof(mystruct_t));

TIA,
Roubles
Nov 14 '05 #1
4 9417
Roubles wrote:

Hi All,

Quick question; what is the difference between initializing and
clearing a structure like this:

mystruct_t a = {0};

and initializing and clearing it like this:

mystruct_t a;

memset(a, 0, sizeof(mystruct_t));


In practice, probably not much.

However, suppose the first element of the struct were a pointer, and
you are on a system where a NULL pointer is not all-bits-zero.

Also, my understanding is that _all_ entries will be set to their zero
representation, which may not be all-bits-zero. (For example, a "double"
value.)

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Nov 14 '05 #2
Roubles wrote:

Hi All,

Quick question; what is the difference between initializing and
clearing a structure like this:

mystruct_t a = {0};
This sets each member of the structure to the equivalent of 0 for the
type. You can only intitialize this way, not assign.
and initializing and clearing it like this:

mystruct_t a;

memset(a, 0, sizeof(mystruct_t));

This sets the whole thing to all-bits 0. For some things like floating
point numbers or pointers, this could be the wrong thing.

Brian Rodenborn
Nov 14 '05 #3
Roubles wrote:
Hi All,

Quick question; what is the difference between initializing and
clearing a structure like this:

mystruct_t a = {0};
and initializing and clearing it like this:

mystruct_t a;

memset(a, 0, sizeof(mystruct_t));

TIA,
Roubles


The first is an initialization the compiler does for you and the
second is one you do yourself. You know that. What is the real
question? As an aside, I note that the prototype..

void *memset(void *buffer, int ch, size_t num);

...suggests to me..

memset(&a, 0, sizeof a);

...but of course, it's just style, right?
--
Joe Wright mailto:jo********@comcast.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #4
Roubles wrote:
Hi All,

Quick question; what is the difference between initializing and
clearing a structure like this:

mystruct_t a = {0};
and initializing and clearing it like this:

mystruct_t a;

memset(a, 0, sizeof(mystruct_t));

TIA,
Roubles


Note that a is a structure, not a pointer. You need &a to take its
address for memset().
--
Joe Wright mailto:jo********@comcast.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Skybuck Flying | last post: by
2 posts views Thread by kimimaro | last post: by
1 post views Thread by int2str | last post: by
5 posts views Thread by aarklon | last post: by
11 posts views Thread by aaragon | last post: by
3 posts views Thread by Martin | last post: by
17 posts views Thread by jb.simon | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.