473,394 Members | 1,737 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

memset all bits to zero will let float/double to zero?

Hi, All
I am reading FAQ of this group. I have a question about this:
http://www.eskimo.com/~scs/C-faq/q7.31.html

It says:
"
p = malloc(m * n);
memset(p, 0, m * n);
The zero fill is all-bits-zero, and does not therefore guarantee useful null
pointer values (see section 5 of this list) or floating-point zero values.
"

but ieee754 defined float zero to be all-bits-zero.

Anyone can help to explain about this?

Thanks in advance.
Nov 14 '05
53 8061
Da*****@cern.ch (Dan Pop) writes:
In <ln************@nuthaus.mib.org> Keith Thompson <ks***@mib.org> writes: [...]
Yes, I meant null pointer constants. I didn't badly confuse them; I
made a minor error.

Yes, passing 0 (or NULL, if it's #defined as 0) to a non-prototyped
function expecting a pointer is an error. The fact remains that there
is code in the real world that makes this error, and happens to work
as expected on platforms where pointers are the same size as int and
the null pointer has the same representation as (int)0.


This is not enough to make it work. If the platform has separate data
and address registers, the value *may* be passed in the "wrong" register
and the code will fail miserably.


Fine, I didn't exhaustively list all the criteria necessary for such
code to work. My point (and it was a minor one) was that such code
does happen to "work" on some platforms, and some implementers *might*
cater to such code.

[snip]
I don't know why you keep invoking severely broken code that happens to
work by accident as an argument. Please show us some *concrete* evidence
that implementors care about such code when making their decisions and
that they're ready to sacrifice performance in order to keep broken code
working as intended by its developer.


I have no such concrete examples. I know of no real world
architecture on which there's a significant performance difference
between using all-bits-zero for null pointers and using some other
representation. (Though if using all-bits-zero also makes it easier
to catch errors, that might be a reason to use all-bits-zero even if
there is a slight performance penalty.)

I do have a couple of speculative examples where implementors *might*
have catered to "broken" code, though perhaps not at the cost of a
performance penalty. For most implementations, the argument passing
convention is such that printf() happens to work in many cases even
without a valid prototype. For most implementations, null pointers
are represented as all-bits-zero, even though using a different
representation would only break invalid code. Perhaps that's just
inertia.

But here's the actual context. Upthread, you wrote:
>> If all bits zero is an inconvenient representation for the null
>> pointer on a given platform, no implementor is going to use it
>> and there is no point in explaining how it could be used,
>> anyway.


I responded that I'm not convinced, and presented some possible
reasons why some implementors *might* do that. Perhaps nobody
actually would; at this point, I don't particularly care, and I doubt
that anyone else still following this thread does either.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #51

"Keith Thompson" <ks***@mib.org> wrote in message

For most implementations, null pointers are represented as all-
bits-zero, even though using a different representation would
only break invalid code. Perhaps that's just inertia.

I think the main reason is psychological. foo(0) passes a NULL pointer to
foo(), so an implementor is going to use all bits zero as the representation
unless there is a really pressing reason to do otherwise. Then there's the
problem of code that uses memset() or calloc() to intialise pointer data.

Nov 14 '05 #52
"Malcolm" <ma*****@55bank.freeserve.co.uk> wrote:
"Keith Thompson" <ks***@mib.org> wrote in message

For most implementations, null pointers are represented as all-
bits-zero, even though using a different representation would
only break invalid code. Perhaps that's just inertia.
I think the main reason is psychological. foo(0) passes a NULL pointer to
foo(), so an implementor is going to use all bits zero as the representation
unless there is a really pressing reason to do otherwise.


Huh? Non sequitur, methinks.
Then there's the
problem of code that uses memset() or calloc() to intialise pointer data.


The problem is in that code, not in the non-zero representation of a
null pointer where this is useful.

Richard
Nov 14 '05 #53
In <ln************@nuthaus.mib.org> Keith Thompson <ks***@mib.org> writes:
But here's the actual context. Upthread, you wrote:
>> If all bits zero is an inconvenient representation for the null
>> pointer on a given platform, no implementor is going to use it
>> and there is no point in explaining how it could be used,
>> anyway.


I responded that I'm not convinced, and presented some possible
reasons why some implementors *might* do that. Perhaps nobody
actually would; at this point, I don't particularly care, and I doubt
that anyone else still following this thread does either.


So, you're adamantly trying to make a pointless point, as long as you
can produce no concrete evidence supporting your position. A "might"
without any kind of support behind (except pure speculation, with no
supporting evidence) is worthless in any technical debate.

It was one of the extremely rare cases when Malcolm actually made a
sensible statement and you chose to disagree for no *good* reason
(your empty "might"s do not qualify as good reason).

When all other things are equal, implementors are known to make the choice
that preserves as much broken code as possible. But this was NOT the
case for this discussion, which involved cases when choosing all bits
zero for null pointers has performance costs, of one nature or another.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #54

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
by: 69dbb24b2db3daad932c457cccfd6 | last post by:
Hello, I have to initialize all elements of a very big float point array to zero. It seems memset(a, 0, len) is faster than a simple loop. I just want to know whether it is safe to do so, since I...
22
by: silversurfer2025 | last post by:
Hello everybdy, I am a little confused for the following reason: In my code I used a simple for-loop in order to initialize a 2D-array of floats to zero. Because of efficiency reasons, I changed...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.