Asking if elements in struct arre zero

T

Terry Andersen

If I have:

struct one_{
unsigned int one_1;
unsigned short one_2;
unsigned short one_3;
};

struct two_{
unsigned int two_1;
unsigned short two_2;
unsigned char two_3;
};

struct mystruct{
struct one_ one;
struct two_ two;
}mystruct1;

Then could I by any change ask on the value of the whole struct mystruct1,
that is all the elements in the struct in one call? I want to do something
like (in pseudo like language):

if(mystruct1 == 0) { print("All elements of mystruct1 is zero");}


Best Regards
Terry
 
R

Richard Heathfield

Terry said:
If I have:

struct one_{
unsigned int one_1;
unsigned short one_2;
unsigned short one_3;
};

struct two_{
unsigned int two_1;
unsigned short two_2;
unsigned char two_3;
};

struct mystruct{
struct one_ one;
struct two_ two;
}mystruct1;

Then could I by any change ask on the value of the whole struct mystruct1,
that is all the elements in the struct in one call? I want to do something
like (in pseudo like language):

if(mystruct1 == 0) { print("All elements of mystruct1 is zero");}

No.

You could, however, do this:

int CompareMyStructsForEquality(const struct mystruct *ms1,
const struct mystruct *ms2)
{
int same = 0;
if(ms1->one.one_1 == ms2->one.one_1 &&
ms1->one.one_2 == ms2->one.one_2 &&
ms1->one.one_3 == ms2->one.one_3 &&
ms1->two.one_1 == ms2->two.one_1 &&
ms1->two.one_2 == ms2->two.one_2 &&
ms1->two.one_3 == ms2->two.one_3)
{
same = 1;
}
return same;
}

You can then do:

struct mystruct z = {0};
if(CompareMyStructsForEquality(&z, &mystruct1) == 1)
{
puts("mystruct1 is zeroed.");
}
 
J

James Hu


[good stuff snipped]

If you knew for sure that struct mystruct did not have any padding bits
(and you can not portably determine this), or if you knew that struct
mystruct is always initialized in a way such that all its padding bits
are zero (such as allocated by calloc(), or was initialized by a call to
memset(p, '\0', sz)), you could have used the function memcmp().

{
static const struct mystruct z;
if(memcmp(&mystruct1, &z)) { print("All elements of mystruct1 is zero"); }
}

But, Richard's suggestion is the most straight forward and less
error prone.

-- James
 
S

Serve Lau

Richard Heathfield said:
You can then do:

struct mystruct z = {0};
if(CompareMyStructsForEquality(&z, &mystruct1) == 1)
{
puts("mystruct1 is zeroed.");
}

Aha, I've found a good use for immediate structs.

if(CompareMyStructsForEquality(&z, &(struct mystruct){0}) == 1)
.....
 
R

Richard Heathfield

James Hu wrote:

If you knew for sure that struct mystruct did not have any padding bits
(and you can not portably determine this), or if you knew that struct
mystruct is always initialized in a way such that all its padding bits
are zero (such as allocated by calloc(), or was initialized by a call to
memset(p, '\0', sz)), you could have used the function memcmp().

{
static const struct mystruct z;
if(memcmp(&mystruct1, &z)) { print("All elements of mystruct1 is zero");

I think you'll need sizeof z as a third argument. :)
 
J

Jeremy Yallop

James said:
If you knew for sure that struct mystruct did not have any padding bits
(and you can not portably determine this)

Why can't you portably determine this? In particular, what's to stop
you from calculating the number of bits in each integer type (using
sizeof and CHAR_BIT) and then comparing the maximum value that can be
represented with that numbers of bits to the actual maximum value for
the type?

Padding /bytes/ in the structure can be detected by comparing the size
of the structure with the sum of the sizes of its members, of course.

Jeremy.
 
R

Roose

I'm not familiar with this {0} syntax. This creates a struct of the correct
type where all the members have value 0? Is this C99 or something?
 
R

Richard Heathfield

[Please don't top-post. It's very irritating. Fixed.]
I'm not familiar with this {0} syntax. This creates a struct of
the correct type where all the members have value 0? Is this
C99 or something?

The syntax struct mystruct z = {0}; is straight C90 - you can use it now,
today. When you partially initialise an aggregate (such as an array or
struct) with *at least* one element, it is the compiler's job to initialise
everything else to static defaults, which are basically 0, 0.0 and NULL.

The construct &(struct mystruct){0}, which uses a "compound literal", is
C99-only.
 
S

Serve Lau

Roose said:
I'm not familiar with this {0} syntax. This creates a struct of the correct
type where all the members have value 0? Is this C99 or something?

yes, although named struct also worked in C89

struct mystruct x = {0};

x gets initialized according to the initializer list. If x has more members
than there are items in the initializer list they will be filled with 0. So
x = {0} effectively fills the struct with 0 and my code example does it with
a nameless struct.
compared to memset(&x, 0 sizeof x); this is the most portable construct,
least to type and a compiler can optimize the most out of it. So why not use
it? :)
 
R

Roose

Thank you for your answer, that cleared it up nicely.

Please don't bottom-post, I find it irritating since when reading a thread,
I basically scroll through/read O(n^2) posts rather than O(n). I find it
_extremely_ irritating when people complain about top-posting, as if they
were the President of UseNet. Even after a decade or more reading it.

Richard Heathfield said:
[Please don't top-post. It's very irritating. Fixed.]
 
R

Richard Heathfield

Roose said:
Thank you for your answer, that cleared it up nicely.

No problem.
Please don't bottom-post, I find it irritating since when reading a
thread,

You like reading upside-down?
I basically scroll through/read O(n^2) posts rather than O(n).

Then encourage people to snip properly, and snip properly yourself to set an
example.

FCOL.
 
J

James Hu

Why can't you portably determine this? In particular, what's to stop
you from calculating the number of bits in each integer type (using
sizeof and CHAR_BIT) and then comparing the maximum value that can be
represented with that numbers of bits to the actual maximum value for
the type?

I had a misthought. Yes, you can portably determine this, but it would
not be a strictly conforming program.

Thanks,

-- James
 
M

Mark Gordon

Thank you for your answer, that cleared it up nicely.

Please don't bottom-post, I find it irritating since when reading a
thread, I basically scroll through/read O(n^2) posts rather than O(n).
I find it
_extremely_ irritating when people complain about top-posting, as if
they were the President of UseNet. Even after a decade or more
reading it.

<snip>

Bottom posting is the long established convention on Usenet, although on
*some* non-technical groups top posting is permitted. It is also
enshrined, together with snipping, in one of the RFCs.

You are also likely to quickly find yourself ignored by a number of the
more knowledgeable posters here if you persist in top posting.
 
M

Morris Dovey

Roose wrote:

[8<]
Please don't bottom-post, I find it irritating since when
reading a thread, I basically scroll through/read O(n^2) posts
rather than O(n). I find it _extremely_ irritating when
people complain about top-posting
[>8]

Bottom-posting is the established norm in this forum. Proposals
to change that norm have been made from time to time; but there
has been little interest in adopting such proposals.

You can, of course, choose not to read bottom-posted articles;
just as you can choose to top-post your replies. I suspect that
such deviant behavior might not be well-regarded - and that you
might well find yourself standing all alone in the crowd.
 
M

Mark McIntyre

Thank you for your answer, that cleared it up nicely.

Please don't bottom-post, I find it irritating since when reading a thread,
I basically scroll through/read O(n^2) posts rather than O(n).

only if idiots don't Snip the irrelevant material .Sheesh, what is it
with some people? Too lazy to snip?
I find it
_extremely_ irritating when people complain about top-posting,

Yeah? Well round here, top posting is frowned on, so stop it.
as if they
were the President of UseNet. Even after a decade or more reading it.

you should know better.
 
R

Roose

OK, feel free to ignore my posts. Like I said, after a decade of posting in
UseNet, I have never had a problem getting any answer to any question, with
top-posting preferred. I do bottom-post when there are multiple points to
address, but in general I find it to read when people top-post (and snip).
If you have a newsreader made after 1991, it is no problem to track the
thread backward.

Why can't we all be grown-ups and accept that there are things in this big
world that we can't control. I prefer top-posting, but I have never
complained about anyone bottom-posting, and certainly would never go to
absurd lengths like actually rearranging the posts.

I also find the all the sigs with the stupid quotes and URLs rather
annoying, but I've never complained about it.
 
C

Chris Torek

[regarding]
The construct &(struct mystruct){0}, which uses a "compound literal", is
C99-only.

Yes. In this particular case -- comparing, not writing on, the
structure -- you might want to use:

&(const struct mystruct){0}

i.e., add a "const" qualifier, provided the compare() function takes
const-qualified pointers. This not only makes the structure contents
read-only (at least in principle), but also informs the compiler that
it is allowed to share storage with other such "const struct mystruct"s.

In other words, barring particularly tricky optimization, the sequence:

extern void foo(const struct S *, const struct S *);

foo(&(struct S){0}, &(struct S){0});

*must* pass two *different* pointer values to function foo(), while
the call:

foo(&(const struct S){0}, &(const struct S){0});

is allowed (but not required) to pass identical pointer values to
foo(). If foo() consists of:

void foo(const struct S *a, const struct S *b) {
printf("in foo(): have %s pointers\n",
a == b ? "same" : "different");
}

then the difference is visible and you can tell whether your C99
compiler implements the "share readonly compound-literal storage"
optimization.

(Disclaimer: this is all based on a C99 draft, and this sort of
fiddly stuff about precisely when and where storage is allocated is
the kind of thing that changes from draft to draft. :) )
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top