memset to initialize double array?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Joakim Hove

    #1

    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
  • Simon Biber

    #2
    Re: memset to initialize double array?

    Joakim Hove wrote:[color=blue]
    > 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?)[/color]

    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.
    [color=blue]
    > [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.[/color]

    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.

    Comment

    • Joakim Hove

      #3
      Re: memset to initialize double array?



      Simon Biber <news@ralmin.cc > writes:[color=blue]
      > 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.[/color]

      OK - I guessed it was something like that.

      [color=blue]
      > In fact, it would not solve the problem. The initialisation done by
      > calloc has exactly the same effect as memsetting to 0.[/color]

      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

      Comment

      • SM Ryan

        #4
        Re: memset to initialize double array?

        Joakim Hove <hove@ntnu.no > wrote:
        #
        #
        # Simon Biber <news@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?

        Comment

        Working...