473,900 Members | 3,343 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 8262
Da*****@cern.ch (Dan Pop) writes:
In <ln************ @nuthaus.mib.or g> Keith Thompson <ks***@mib.or g> 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_Keit h) 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.or g> 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.u k> wrote:
"Keith Thompson" <ks***@mib.or g> 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.or g> Keith Thompson <ks***@mib.or g> 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
26261
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 know it's danger to initialize NULL pointers this way. But how about floats? Zhang Le
22
26760
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 it to use memset and I get totally different results.. How can this be? Here is the example: float gaborfilter;
0
9997
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10866
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10497
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9671
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8043
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7204
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5891
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6082
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4721
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.