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

I

Ioannis Vranos

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.
 
S

Seebs

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
 
K

Keith Thompson

Seebs said:
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).
 
J

James Dow Allen

* 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
 
J

James Dow Allen

<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
 
L

lawrence.jones

James Dow Allen said:
<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!
 
J

James Dow Allen

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
 
L

lawrence.jones

James Dow Allen said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top