Question about classic stuff - calloc() and memset()

Discussion in 'C Programming' started by Ioannis Vranos, Dec 17, 2010.

  1. As far as I know calloc() sets all bits to zero when successful, and
    memset(s, 0, n) does the same thing to the first n characters of s, by
    zeroing the bytes as unsigned chars.

    K&R2 updated with the errata, has an example on page 167

    int *ip;

    ip= calloc(n, sizeof(int));


    Is this always guaranteed to work? As far as I know, an int with value 0
    may not have all its bits to 0.
    Ioannis Vranos, Dec 17, 2010
    #1
    1. Advertising

  2. Ioannis Vranos

    Seebs Guest

    On 2010-12-17, Ioannis Vranos <> wrote:
    > Is this always guaranteed to work? As far as I know, an int with value 0
    > may not have all its bits to 0.


    I think these days the answer is:

    * An int with all bits zero is guaranteed to have the value 0.
    * Possibly, on a sign-magnitude machine, there could exist some int
    with value 0 which does not have all bits zero.

    I am not aware of anything where it doesn't work to set ints to all zero
    bits, though.

    -s
    --
    Copyright 2010, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
    I am not speaking for my employer, although they do rent some of my opinions.
    Seebs, Dec 17, 2010
    #2
    1. Advertising

  3. Seebs <> writes:
    > On 2010-12-17, Ioannis Vranos <> wrote:
    >> Is this always guaranteed to work? As far as I know, an int with value 0
    >> may not have all its bits to 0.

    >
    > I think these days the answer is:
    >
    > * An int with all bits zero is guaranteed to have the value 0.
    > * Possibly, on a sign-magnitude machine, there could exist some int
    > with value 0 which does not have all bits zero.
    >
    > I am not aware of anything where it doesn't work to set ints to all zero
    > bits, though.


    N1256 6.2.6.2p5:

    For any integer type, the object representation where all the
    bits are zero shall be a representation of the value zero in
    that type.

    This requirement was not in either C90 or C99; it was added
    by Technical Corrigendum #2 in response to Defect Report #263,
    <http://www.open-std.org/jtc1/sc22/wg14/www/docs/dr_263.htm>.

    Though versions of the standard prior to TC2 did not guarantee
    that all-bits-zero is a representation of 0 for all integer types,
    in practice you can safely rely on it (otherwise the committee
    presumably wouldn't have accepted the change so easily).

    --
    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, Dec 17, 2010
    #3
  4. Thank you for your replies.
    Ioannis Vranos, Dec 19, 2010
    #4
  5. On Dec 18, 4:41 am, Seebs <> wrote:
    > * An int with all bits zero is guaranteed to have the value 0.
    > * Possibly, on a sign-magnitude machine, there could exist some int
    >   with value 0 which does not have all bits zero.


    Given multiple representations of zero, I suppose they're
    guaranteed to compare equal, right?
    int z1 = 0, z2 = ~0 /* zero on THIS machine */;
    assert (z1 == z2);

    <another useless factoid from the Jurassic Era>: arithmetic
    on the CDC 6600 was designed to make negative zeros rare
    and easy to get rid of! For example, adding positive zero
    to a negative zero made it positive.</useless factoid>

    James Dow Allen
    James Dow Allen, Dec 19, 2010
    #5
  6. On Dec 19, 1:45 pm, James Dow Allen <> wrote:
    > <another useless factoid from the Jurassic Era>


    I forgot to mention the most useless corollary of this factoid.
    The simple copy instruction
    SB5 B4
    would put all zero bits into B5 if B4 was all 1's !
    It's an abbreviation for
    SB5 B4+B0
    where B0 is the read-only reg hard-wired to all 0-bits.
    To do a simple replicate which replicated *any* value
    SB5 B4-B0
    had to be coded explicitly. I suspect many 6600 programmers
    were unaware of this idiosyncrasy, as few were anal-retentive
    enough to use negative-zero as a special flag to save a
    few microseconds(*).

    (Yes, *micro*seconds; this was not a type for nanoseconds.
    The 6600 was a preposterously SLOW machine, though no one
    thought so at the time. :)

    James Dow Allen
    James Dow Allen, Dec 19, 2010
    #6
  7. Ioannis Vranos

    Guest

    James Dow Allen <> wrote:
    >
    > <another useless factoid from the Jurassic Era>: arithmetic
    > on the CDC 6600 was designed to make negative zeros rare
    > and easy to get rid of! For example, adding positive zero
    > to a negative zero made it positive.</useless factoid>


    Interesting. The only machine I ever used that had negative zero worked
    exactly the opposite way -- most operations that resulted in zero
    produced negative zero. When evaluating an expression, the FORTRAN
    compiler would compute the negative of the expression and then negate
    the result to force a zero result to be positive!
    --
    Larry Jones

    See if we can sell Mom and Dad into slavery for a star cruiser. -- Calvin
    , Dec 20, 2010
    #7
  8. On Dec 21, 12:36 am, wrote:
    > James Dow Allen <> wrote:
    > > <another useless factoid from the Jurassic Era>: arithmetic
    > > on the CDC 6600 was designed to make negative zeros rare
    > > and easy to get rid of!  For example, adding positive zero
    > > to a negative zero made it positive.</useless factoid>

    >
    > Interesting.  The only machine I ever used that had negative zero worked
    > exactly the opposite way -- most operations that resulted in zero
    > produced negative zero.


    They called the 6600's "complement-recomplement arithmetic."
    Hardware solved (A + B) via -(-A + -B).
    I've not worked with a CDC 6x00 since the mother of my
    teenage daughter was born, so it's amazing what I remember
    .... after all, I couldn't tell you what I had for lunch yesterday!

    BTW, one of the most talented engineers on the CDC 6600
    design was named Larry Jones! Any relation?

    James
    James Dow Allen, Dec 20, 2010
    #8
  9. Ioannis Vranos

    Guest

    James Dow Allen <> wrote:
    >
    > They called the 6600's "complement-recomplement arithmetic."
    > Hardware solved (A + B) via -(-A + -B).


    Ah, so it did in hardware what the machine I worked on did in software!

    > BTW, one of the most talented engineers on the CDC 6600
    > design was named Larry Jones! Any relation?


    Not that I know of.
    --
    Larry Jones

    That gives me a FABULOUS idea. -- Calvin
    , Dec 21, 2010
    #9
  10. It is always interesting to read discussions in clc!
    Ioannis Vranos, Dec 21, 2010
    #10
    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. k-man
    Replies:
    4
    Views:
    4,505
    Shelley Hebert
    Dec 18, 2003
  2. Joe C
    Replies:
    5
    Views:
    8,831
    tom_usenet
    Aug 24, 2004
  3. buda

    quick question about memset

    buda, Dec 6, 2004, in forum: C Programming
    Replies:
    2
    Views:
    317
    Jack Klein
    Dec 7, 2004
  4. Replies:
    13
    Views:
    776
    Ben Pfaff
    Feb 11, 2006
  5. Replies:
    27
    Views:
    1,045
    werasm
    Jul 14, 2006
Loading...

Share This Page