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

memset to initialize double array?

P: n/a

Hello,

I have an array of doubles, allocated with

dbarr = malloc(N * sizeof(double));

I then want to set[1] all the elements of the array to zero, this is
currently done with

for (i=0; i < N; i++)
dbarr[i] = 0.0;

Now, I think using memset would be more 'elegant', and it seems very
tempting to use:

memset(dbarr , 0 , N * sizeof(* dbarr));

This works on Linux, but do I have a guarantee that the representation
of 0.0 in a double will always be the same as the appropriate number
of consecutive char zero representations? (I mean in principle some
hardware/software could use 01010101010101010.... to represent zero
for a 64 bit double, and 11110000 for a byte zero - or am I just
rambling with nonsense here?)

Regards Joakim
[1]: I know calloc() would solve the problem in the first go, but
dbarr will be used several times, and must be cleared between
each usage.

--
Joakim Hove
hove AT ntnu.no /
Tlf: +47 (55 5)8 27 13 / Stabburveien 18
Fax: +47 (55 5)8 94 40 / N-5231 Paradis
http://www.ift.uib.no/~hove/ / 55 91 28 18 / 92 68 57 04
Nov 15 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Joakim Hove wrote:
Hello,

I have an array of doubles, allocated with

dbarr = malloc(N * sizeof(double));

I then want to set[1] all the elements of the array to zero, this is
currently done with

for (i=0; i < N; i++)
dbarr[i] = 0.0;

Now, I think using memset would be more 'elegant', and it seems very
tempting to use:

memset(dbarr , 0 , N * sizeof(* dbarr));

This works on Linux, but do I have a guarantee that the representation
of 0.0 in a double will always be the same as the appropriate number
of consecutive char zero representations? (I mean in principle some
hardware/software could use 01010101010101010.... to represent zero
for a 64 bit double, and 11110000 for a byte zero - or am I just
rambling with nonsense here?)
Good question. The answer is no. There is no guarantee in the standard C
language that all-bits-zero is a valid representation of a double value
at all, let alone that that value is equal to 0.0.

On most systems (Unix, Windows, Mac, etc.), the floats and doubles are
close enough to IEEE 754 compliant, and so you will not run into problems.

You also need to be aware that writing all-zeros into a pointer type
does not necessarily produce a valid representation of a null pointer,
either.
[1]: I know calloc() would solve the problem in the first go, but
dbarr will be used several times, and must be cleared between
each usage.


In fact, it would not solve the problem. The initialisation done by
calloc has exactly the same effect as memsetting to 0. You gain nothing.
It is not safe to expect zero floats or doubles, or null pointers, from
calloc.

--
Simon.
Nov 15 '05 #2

P: n/a


Simon Biber <ne**@ralmin.cc> writes:
Good question. The answer is no. There is no guarantee in the standard C
language that all-bits-zero is a valid representation of a double value
at all, let alone that that value is equal to 0.0.

On most systems (Unix, Windows, Mac, etc.), the floats and doubles are
close enough to IEEE 754 compliant, and so you will not run into problems.
OK - I guessed it was something like that.

In fact, it would not solve the problem. The initialisation done by
calloc has exactly the same effect as memsetting to 0.


Well, that was indeed news!
Thanks for answering!
Joakim

--
Joakim Hove
hove AT ntnu.no /
Tlf: +47 (55 5)8 27 13 / Stabburveien 18
Fax: +47 (55 5)8 94 40 / N-5231 Paradis
http://www.ift.uib.no/~hove/ / 55 91 28 18 / 92 68 57 04
Nov 15 '05 #3

P: n/a
Joakim Hove <ho**@ntnu.no> wrote:
#
#
# Simon Biber <ne**@ralmin.cc> writes:
# > Good question. The answer is no. There is no guarantee in the standard C
# > language that all-bits-zero is a valid representation of a double value
# > at all, let alone that that value is equal to 0.0.
# >
# > On most systems (Unix, Windows, Mac, etc.), the floats and doubles are
# > close enough to IEEE 754 compliant, and so you will not run into problems.
#
# OK - I guessed it was something like that.
#
#
# > In fact, it would not solve the problem. The initialisation done by
# > calloc has exactly the same effect as memsetting to 0.
#
# Well, that was indeed news!

On any system you're likely to use, memset to 0 works.
On any system you can possibly meet, it does not.
You can program for what you're likely to meet, or use a
more restricted language for anything you can possibly meet.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
What kind of convenience store do you run here?
Nov 15 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.