memset to initialize double array?

J

Joakim Hove

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 = 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
 
S

Simon Biber

Joakim said:
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 = 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.
 
J

Joakim Hove

Simon Biber said:
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
 
S

SM Ryan

#
#
# > 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.
 

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

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top