By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,640 Members | 1,598 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,640 IT Pros & Developers. It's quick & easy.

How can I set a integer array all bits to zero?

P: n/a

#include <string.h>

int a[ 100 ];

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

Does that guarantee all bits zero?

--
|
___
(-_-)
<| |>---------------------------------- ShepJeng.twbbs.org -------------
/ plum.cs.nccu.edu.tw
Nov 13 '05 #1
Share this Question
Share on Google+
15 Replies


P: n/a
I wish wrote:
#include <string.h>

int a[ 100 ];

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

Does that guarantee all bits zero?


Depends, C90 no, C99 yes.

Both: int a[100] = {0};

Jirka

Nov 13 '05 #2

P: n/a
Jirka Klaue wrote:
I wish wrote:
#include <string.h>

int a[ 100 ];

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

Does that guarantee all bits zero?

Depends, C90 no, C99 yes.

You piqued my curiosity. What's up with C99 that the memset guarantees
all bits zero ?
Both: int a[100] = {0};

--
Bertrand Mollinier Toublet
"Lead developer. Iron developer. Copper developer." -- Sean Egan

Nov 13 '05 #3

P: n/a
Jirka Klaue wrote:

I wish wrote:
#include <string.h>

int a[ 100 ];

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

Does that guarantee all bits zero?


Depends, C90 no, C99 yes.


There's no "depends" about it: under all versions of
the Standard, `a' is set to all bits zero by this code.

What *does* depend on the Standard version is whether
all bits zero is the same as value zero. In C89/90, an
`int' with all bits zero has the value zero. In C99,
where an `int' is permitted to contain bits that are not
part of its value ("padding bits"), it is possible that a
zero-valued `int' might contain non-zero bits, and it
is also possible that an `int' with all bits zero might
be a "trap representation."

--
Er*********@sun.com
Nov 13 '05 #4

P: n/a
Eric Sosman wrote:
Jirka Klaue wrote: ....
int a[ 100 ];
memset( a, 0, sizeof(a) );

Does that guarantee all bits zero?


Depends, C90 no, C99 yes.


There's no "depends" about it: under all versions of
the Standard, `a' is set to all bits zero by this code.

What *does* depend on the Standard version is whether
all bits zero is the same as value zero.


That's what I meant. Sorry.
In C89/90, an
`int' with all bits zero has the value zero. In C99,
where an `int' is permitted to contain bits that are not
part of its value ("padding bits"), it is possible that a
zero-valued `int' might contain non-zero bits, and it
is also possible that an `int' with all bits zero might
be a "trap representation."


I thought, it is the other way round. Hmm...

Jirka

Nov 13 '05 #5

P: n/a
> What *does* depend on the Standard version is whether
all bits zero is the same as value zero. In C89/90, an
`int' with all bits zero has the value zero. In C99,
where an `int' is permitted to contain bits that are not
part of its value ("padding bits"), it is possible that a
zero-valued `int' might contain non-zero bits, and it
is also possible that an `int' with all bits zero might
be a "trap representation."

Padding bits in int values? What reason could made an implementation do
this?

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
Nov 13 '05 #6

P: n/a
In article <bm************@ID-176797.news.uni-berlin.de>,
"cody" <do*********************@gmx.de> wrote:
cody

[Freeware, Games and Humor]


But definitely not funny.
Nov 13 '05 #7

P: n/a
"cody" <do*********************@gmx.de> wrote:
Padding bits in int values? What reason could made an implementation do
this?


Parity bits, for example.
--
Irrwahn
(ir*******@freenet.de)
Nov 13 '05 #8

P: n/a

"Eric Sosman" <Er*********@sun.com> wrote in message news:3F***************@sun.com...
Jirka Klaue wrote:

I wish wrote:
#include <string.h>

int a[ 100 ];

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

Does that guarantee all bits zero?
Depends, C90 no, C99 yes.


There's no "depends" about it: under all versions of
the Standard, `a' is set to all bits zero by this code.


The representations of integer types in C90 are vague because C90
doesn't say much. But we can know that unsigned char in C90 can have
padding bits from a relevant DR, even if it's not the committee's real
intent. Of course, with lack of enough description I think it's
useless to discuss the integer representations in C90.

What *does* depend on the Standard version is whether
all bits zero is the same as value zero. In C89/90, an
`int' with all bits zero has the value zero.


If the reason for this is because C90 doesn't mention padding bits for
integer types, the story changed after the DR that I mentioned above
and on which C99's model was based was published.
--
Jun, Woong (mycoboco at hanmail.net)
Nov 13 '05 #9

P: n/a
On Wed, 15 Oct 2003 11:49:53 -0400, Eric Sosman <Er*********@sun.com>
wrote in comp.lang.c:
Jirka Klaue wrote:

I wish wrote:
#include <string.h>

int a[ 100 ];

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

Does that guarantee all bits zero?


Depends, C90 no, C99 yes.


There's no "depends" about it: under all versions of
the Standard, `a' is set to all bits zero by this code.

What *does* depend on the Standard version is whether
all bits zero is the same as value zero. In C89/90, an
`int' with all bits zero has the value zero. In C99,
where an `int' is permitted to contain bits that are not
part of its value ("padding bits"), it is possible that a
zero-valued `int' might contain non-zero bits, and it
is also possible that an `int' with all bits zero might
be a "trap representation."


Chapter and verse, please, that C90 guaranteed that all bits 0 is a
valid representation of the value 0 for _ANY_ type.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Nov 13 '05 #10

P: n/a
Jack Klein wrote:

On Wed, 15 Oct 2003 11:49:53 -0400, Eric Sosman <Er*********@sun.com>
wrote in comp.lang.c:
What *does* depend on the Standard version is whether
all bits zero is the same as value zero. In C89/90, an
`int' with all bits zero has the value zero. [...]


Chapter and verse, please, that C90 guaranteed that all bits 0 is a
valid representation of the value 0 for _ANY_ type.


Well, I might be wrong (it's happened before, and will
happen again). The reasoning goes like this: C90 requires
a "pure binary numeration system" for integer types, and
makes no mention of the possibility of non-value bits. If
there are no padding bits, all bits zero must be a valid
representation of an `int' zero.

The weak spot in the argument, of course, is the step
from "C90 doesn't mention padding bits" to the conclusion
"padding bits do not exist." It's logically unsound, I
readily admit. But C90 also omits any mention of behavior
that might result from messing with the representation of
an `int', and C90 also specifically allows a `char' with
all bits zero, so it seems likely -- not conclusive, but
likely -- that C90 doesn't permit padding bits.

By way of analogy (another fallacious, but suggestive
approach), consider that C99 makes no mention of radioactive
bits, or of any behavior that might result from using an
`int' containing one or more radioactive bits. It seems
reasonable to conclude that C99 doesn't permit radioactive
bits, even though Cnext might introduce them.

--
Er*********@sun.com
Nov 13 '05 #11

P: n/a
On Thu, 16 Oct 2003 00:13:26 +0200, "cody" <do*********************@gmx.de>
wrote:
Padding bits in int values? What reason could made an implementation do
this?


It's unlikely to be done just for fun, but for instance, some obscure
machines have no hardware support for integer operations - everything has to
be done using floating-point. The mantissa bits can store the value, the
sign bit does the obvious, and the exponent becomes padding. On such a
machine, all-bits-zero (including padding) could well be a trap
representation.

-- Mat.

Nov 13 '05 #12

P: n/a
# Padding bits in int values? What reason could made an implementation do
# this?

Star 100.

--
Derk Gwen http://derkgwen.250free.com/html/index.html
If you plan to shoplift, let us know.
Thanks
Nov 13 '05 #13

P: n/a
"Derk Gwen" <de******@HotPOP.com> schrieb im Newsbeitrag
news:vo************@corp.supernews.com...
# Padding bits in int values? What reason could made an implementation do
# this?

Star 100.

Huh?

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
Nov 13 '05 #14

P: n/a
On Sat, 18 Oct 2003 01:43:19 +0200, in comp.lang.c , "cody"
<do*********************@gmx.de> wrote:
"Derk Gwen" <de******@HotPOP.com> schrieb im Newsbeitrag
news:vo************@corp.supernews.com...
# Padding bits in int values? What reason could made an implementation do
# this?

Star 100.

Huh?


its an implementation.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 13 '05 #15

P: n/a
"Mark McIntyre" <ma**********@spamcop.net> schrieb im Newsbeitrag
news:k6********************************@4ax.com...
On Sat, 18 Oct 2003 01:43:19 +0200, in comp.lang.c , "cody"
<do*********************@gmx.de> wrote:
"Derk Gwen" <de******@HotPOP.com> schrieb im Newsbeitrag
news:vo************@corp.supernews.com...
# Padding bits in int values? What reason could made an implementation do # this?

Star 100.

Huh?


its an implementation.

And why do they insert padding bits? What sizes are their integers?

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
Nov 13 '05 #16

This discussion thread is closed

Replies have been disabled for this discussion.