Re: "memset" vs "= {0}"...Are they equivalent if your initializing variables?

Discussion in 'C++' started by Guest, Sep 23, 2004.

  1. Guest

    Guest Guest

    On Wed, 22 Sep 2004 14:02:56 GMT, JKop <> wrote:


    >
    >As regards pointers... well let's say that on a certain system, a pointer
    >variable takes up 32 bits in memory, as so:
    >
    >
    >0000 0000 0000 0000 0000 0000 0000 0000
    >
    >
    >What you're looking at above is "all bits zero". On Windows, this indicates
    >that the pointer is a null pointer. Now imagine a system where the memory
    >address 0 is a valid one, ie. it's the first byte of memory, and that on
    >this particular system, the null pointer value is:
    >
    >1111 1111 1111 1111 1111 1111 1111 1111
    >
    >
    >When you write a program with the following line in it:
    >
    >
    >int* p_k = 0;
    >
    >The compiler doesn't produce code that sets all bits to zero... no no...
    >what it does is produce code that sets it to the null pointer value for that
    >system (and/or for that type, I believe systems may choose to have different
    >null pointer values depending on the type...). But "memset" doesn't have a
    >clue about this, all it does is set all bits to zero, which may be a valid
    >memory address on some systems, hence it's not portable.
    >
    >And as regards floating point numbers, implementations aren't obligated to
    >represent the value zero as "all bits zero" either.
    >
    >I don't see how either of the three could be slower/faster than each other,
    >they should all yield the same machine code (except maybe the call to
    >"memeset" might add overhead if it's not inline...)
    >
    >
    >-JKop




    Thank you. Very clear now on why not to always rely on memset.

    -Nollie
    Guest, Sep 23, 2004
    #1
    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. Replies:
    17
    Views:
    6,498
    Greg Comeau
    Sep 22, 2004
  2. Guest
    Replies:
    1
    Views:
    426
    Ron Natalie
    Sep 23, 2004
  3. Asfand Yar Qazi
    Replies:
    4
    Views:
    396
    Asfand Yar Qazi
    Nov 12, 2004
  4. Martin M.
    Replies:
    4
    Views:
    341
    Simon Brunning
    Dec 15, 2005
  5. Dave Rudolf
    Replies:
    1
    Views:
    300
    Kai-Uwe Bux
    May 17, 2006
Loading...

Share This Page