Does memory copied with memcpy() match bit for bit?

Discussion in 'C Programming' started by Spiros Bousbouras, Jan 15, 2009.

  1. In a different post ([1]) I suggested the possibility of using
    memcpy() to copy regions of memory (to be) used by other
    processes. It would be useful for this sort of thing to know
    that the regions will match bit for bit but I don't think that
    the standard guarantees that. For example 6.2.6.2. p3 says

    It is unspecified whether these cases actually
    generate a negative zero or a normal zero, and
    whether a negative zero becomes a normal zero when
    stored in an object.

    The part that worries me is what comes after the comma.
    Unitialised memory might contain a negative zero which might
    become normal zero and I imagine the difference might be
    important for some other programme. So what are your views?


    [1] Link is http://tinyurl.com/9t9mmd but you have to click on
    my name, or at least that's how it works for me.
     
    Spiros Bousbouras, Jan 15, 2009
    #1
    1. Advertising

  2. Spiros Bousbouras

    Guest

    On Jan 15, 3:49 pm, Spiros Bousbouras <> wrote:
    > In a different post ([1]) I suggested the possibility of using
    > memcpy() to copy regions of memory (to be) used by other
    > processes.  It would be useful for this sort of thing to know
    > that the regions will match bit for bit but I don't think that
    > the standard guarantees that. For example 6.2.6.2. p3 says
    >
    >     It is unspecified whether these cases actually
    >     generate a negative zero or a normal zero, and
    >     whether a negative zero becomes a normal zero when
    >     stored in an object.
    >
    > The part that worries me is what comes after the comma.
    > Unitialised memory might contain a negative zero which might
    > become normal zero and I imagine the difference might be
    > important for some other programme. So what are your views?
    >
    > [1] Link ishttp://tinyurl.com/9t9mmdbut you have to click on
    > my name, or at least that's how it works for me.


    You haven't quoted 6.2.6.2 p3 fully:
    <quote>
    If the implementation supports negative zeros, they shall be generated
    only by:
    -- the &, |, ^, ~, <<, and >> operators with arguments that produce
    such a value;
    -- the +, -, *, /, and % operators where one argument is a negative
    zero and the result is
    zero;
    -- compound assignment operators based on the above cases.
    It is unspecified whether these cases actually generate a negative
    zero or a normal zero,
    and whether a negative zero becomes a normal zero when stored in an
    object.
    </quote>

    To answer your question, yes they match bit by bit. The standard
    allows you to peek at an objects representation with unsigned char *.
    I hope I'm clear enough, if not, here's pseudocode:
    T obj; ((unsigned char *)&obj)[0 ... sizeof obj - 1]
    See 6.2.6 Representation of types
     
    , Jan 15, 2009
    #2
    1. Advertising

  3. Spiros Bousbouras

    James Kuyper Guest

    Spiros Bousbouras wrote:
    > In a different post ([1]) I suggested the possibility of using
    > memcpy() to copy regions of memory (to be) used by other
    > processes. It would be useful for this sort of thing to know
    > that the regions will match bit for bit but I don't think that
    > the standard guarantees that. For example 6.2.6.2. p3 says
    >
    > It is unspecified whether these cases actually
    > generate a negative zero or a normal zero, and
    > whether a negative zero becomes a normal zero when
    > stored in an object.
    >
    > The part that worries me is what comes after the comma.
    > Unitialised memory might contain a negative zero which might
    > become normal zero and I imagine the difference might be


    The behavior of memcpy() is described in 7.21. 7.21.1p3 says

    "For all functions in this subclause, each character shall be
    interpreted as if it had the type unsigned char (and therefore every
    possible object representation is valid and has a different value)."

    As a result, signed zeros are not relevant, and no such conversion is
    allowed.
     
    James Kuyper, Jan 15, 2009
    #3
  4. James Kuyper <> writes:
    > Spiros Bousbouras wrote:
    >> In a different post ([1]) I suggested the possibility of using
    >> memcpy() to copy regions of memory (to be) used by other
    >> processes. It would be useful for this sort of thing to know
    >> that the regions will match bit for bit but I don't think that
    >> the standard guarantees that. For example 6.2.6.2. p3 says
    >> It is unspecified whether these cases actually
    >> generate a negative zero or a normal zero, and
    >> whether a negative zero becomes a normal zero when
    >> stored in an object.
    >> The part that worries me is what comes after the comma.
    >> Unitialised memory might contain a negative zero which might
    >> become normal zero and I imagine the difference might be

    >
    > The behavior of memcpy() is described in 7.21. 7.21.1p3 says
    >
    > "For all functions in this subclause, each character shall be
    > interpreted as if it had the type unsigned char (and therefore every
    > possible object representation is valid and has a different value)."
    >
    > As a result, signed zeros are not relevant, and no such conversion is
    > allowed.


    That paragraph doesn't exist in the original C99 standard; it was
    added by TC2. (Which, as I understand it, is just as official as the
    original standard.)

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Jan 16, 2009
    #4
  5. Spiros Bousbouras

    Guest

    On 15 Jan., 14:49, Spiros Bousbouras <> wrote:
    > In a different post ([1]) I suggested the possibility of using
    > memcpy() to copy regions of memory ...


    The function memcpy() was designed to copy memory
    such that the memory copied matches bit for bit.
    If it would not work that way many programs would
    have trouble.

    Greetings Thomas Mertes

    Seed7 Homepage: http://seed7.sourceforge.net
    Seed7 - The extensible programming language: User defined statements
    and operators, abstract data types, templates without special
    syntax, OO with interfaces and multiple dispatch, statically typed,
    interpreted or compiled, portable, runs under linux/unix/windows.
     
    , Jan 16, 2009
    #5
    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. Gaijinco
    Replies:
    4
    Views:
    265
    Eric Jensen
    Jul 3, 2006
  2. ziibrs
    Replies:
    2
    Views:
    357
    ziibrs
    Mar 16, 2007
  3. Martin
    Replies:
    25
    Views:
    1,417
    Hallvard B Furuseth
    May 27, 2007
  4. Spiros Bousbouras

    memcpy() with unitialised memory

    Spiros Bousbouras, Jan 14, 2009, in forum: C Programming
    Replies:
    48
    Views:
    1,284
    Mark Wooding
    Jan 18, 2009
  5. Jerry Krinock
    Replies:
    7
    Views:
    168
    Uri Guttman
    Mar 8, 2010
Loading...

Share This Page