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

  2. Guest

    Ron Natalie Guest

    <> wrote in message news:udA4d.57488$...
    > >
    > >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:

    Actually on lots of machines (including the Windows platforms), location zero is
    as good of a memory location as anything else. It's the C/C++ implemenation on
    these machines that chooses not to locate anything (accessible) there so it can use all zeros as
    the null pointer.
     
    Ron Natalie, Sep 23, 2004
    #2
    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,521
    Greg Comeau
    Sep 22, 2004
  2. Guest
    Replies:
    0
    Views:
    398
    Guest
    Sep 23, 2004
  3. Asfand Yar Qazi
    Replies:
    4
    Views:
    404
    Asfand Yar Qazi
    Nov 12, 2004
  4. Martin M.
    Replies:
    4
    Views:
    347
    Simon Brunning
    Dec 15, 2005
  5. Dave Rudolf
    Replies:
    1
    Views:
    306
    Kai-Uwe Bux
    May 17, 2006
Loading...

Share This Page