473,804 Members | 3,722 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 010101010101010 10.... 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 26516
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 010101010101010 10.... 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
26235
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 know it's danger to initialize NULL pointers this way. But how about floats? Zhang Le
21
8796
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 pointer to the start, the value (most of the time zero) and the size of the memory array to clear. Problems appear when the size given is not the size of the object given as its first argument. For instance void fn(void)
9
12385
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
5238
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 benefit of initializing this way? Does it work in this example? Does it work in general ? Is it a good idea in general? class A { public:
22
26749
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 it to use memset and I get totally different results.. How can this be? Here is the example: float gaborfilter;
23
13335
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
9908
by: stn2007 | last post by:
#define VECTOR_LENGTH 255 #define MAX_RAD 15 #define MAX_ANGLE 15 double FR; double FI; condition a
18
783
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
2500
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 are still a garbage value "-16843010" why?
0
9704
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10318
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10302
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10069
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9130
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5503
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3802
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2975
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.