473,413 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

memset to initialize double array?


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
3 26390
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


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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
by: 69dbb24b2db3daad932c457cccfd6 | last post by:
Hello, I have to initialize all elements of a very big float point array to zero. It seems memset(a, 0, len) is faster than a simple loop. I just want to know whether it is safe to do so, since I...
21
by: jacob navia | last post by:
Many compilers check printf for errors, lcc-win32 too. But there are other functions that would be worth to check, specially memset. Memset is used mainly to clear a memory zone, receiving a...
9
by: divya_rathore_ | last post by:
Consider the dynamic allocation of a 2D array: int **temp; temp = new int*; for (y=0; y<height; y++) temp = new int; I have 2 Questions: Does new initializes memory to 0?
27
by: volunteers | last post by:
I met a question about memset and have no idea right now. Could anybody give a clue? Thanks memset is sometimes used to initialize data in a constructor like the example below. What is the...
22
by: silversurfer2025 | last post by:
Hello everybdy, I am a little confused for the following reason: In my code I used a simple for-loop in order to initialize a 2D-array of floats to zero. Because of efficiency reasons, I changed...
23
by: AndersWang | last post by:
Hi, dose anybody here explain to me why memset would be faster than a simple loop. I doubt about it! In an int array scenario: int array; for(int i=0;i<10;i++) //ten loops
1
by: stn2007 | last post by:
#define VECTOR_LENGTH 255 #define MAX_RAD 15 #define MAX_ANGLE 15 double FR; double FI; condition a
18
by: Gaijinco | last post by:
I'm having a headache using memset() Given: int v; memset((void*)v, 1, sizeof(v)); Can I be 100% positive than v = 1 for i 0, or there is something else I have to do?.
6
by: Vincent SHAO | last post by:
int distance; memset(distance,-2,MAX_X*MAX_Y*sizeof(int)); Academically, the buffer "distance" should have been filled with -2 after these two line, but, memset just fail to do the trick, they...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.