memset to initialize double array?

Discussion in 'C Programming' started by Joakim Hove, Nov 8, 2005.

  1. Joakim Hove

    Joakim Hove Guest

    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
     
    Joakim Hove, Nov 8, 2005
    #1
    1. Advertising

  2. Joakim Hove

    Simon Biber Guest

    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 = 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.
     
    Simon Biber, Nov 8, 2005
    #2
    1. Advertising

  3. Joakim Hove

    Joakim Hove Guest

    Simon Biber <> 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
     
    Joakim Hove, Nov 8, 2005
    #3
  4. Joakim Hove

    SM Ryan Guest

    Joakim Hove <> wrote:
    #
    #
    # Simon Biber <> 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?
     
    SM Ryan, Nov 8, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sydex
    Replies:
    12
    Views:
    6,501
    Victor Bazarov
    Feb 17, 2005
  2. Zhiqiang Ye
    Replies:
    53
    Views:
    10,273
    Dan Pop
    Jun 28, 2004
  3. silversurfer2025
    Replies:
    22
    Views:
    3,091
    Jerry Coffin
    Jul 29, 2006
  4. Replies:
    5
    Views:
    432
    James Kanze
    Jun 27, 2008
  5. Replies:
    5
    Views:
    4,216
    CBFalconer
    Dec 29, 2008
Loading...

Share This Page