I searched the FAQ on this one, couldn't really find an answer.
Stylistically, is it better to use 0 or NULL?
I know that pre-Standard, 0 was recommended, because some compilers
implemented NULL improperly.
However, 18.1/4(footnote 180) indicates that 0 and 0L are valid NULLs,
while (void*)0 is explicitly invalid.
So, assuming a (reasonably) Standard compliant compiler, is there a
reason to continue to prefer 0 over NULL? NULL would seem to have the
advantage of indicating that we're checking a pointer.
Also, did nullptr make it into the C++0x draft?
Jun 21 '06
37 7030
Roy Smith wrote: Also, I can easily search for the string NULL in my source code. You don't realize how important stuff like that is until you've got a million lines of source code to slog trough trying to find constructs which might tickle some new compiler bug that was just discovered.
You can use something like:
template <class T> T * NullPtr () { return static_cast <T *> (0); }
template <class T> bool isNull (T * ptr) { return ptr == 0; }
That way you have easy searchability and type safety for a low price.
--
Salu2
Inviato da X-Privat.Org - Registrazione gratuita http://www.x-privat.org/join.php
red floyd wrote: Andrey Tarasevich wrote: red floyd wrote: ... So, assuming a (reasonably) Standard compliant compiler, is there a reason to continue to prefer 0 over NULL? NULL would seem to have the advantage of indicating that we're checking a pointer. ... For that very reason it makes sense to prefer NULL over 0 in pointer context. Otherwise, they are equivalent (in such contexts).
Thanks Alf and Andrey. As I said, the 0 habit was from pre-Standard days, based on old versions of TCPPPL (1st and 2nd editions!!!).
Just curious about prevailing style.
The prevailing style is that it is a religious debate similar to where
you put your brackets, only the case of null, the standard is working
on resolving the debate. Thanks again!
Rolf Magnus wrote: Use NULL to state your intent as clearly and prouncably as possible.
That's self-contradictory. NULL is an integer, but one usually thinks of it as a pointer, because it is only used in pointer context. So using NULL to hide the fact that you have an integer is less clear than directly using 0. http://groups.google.com/groups/search?q=kanze+null
If you think you have a stand here, you owe Kanze a post. ;-)
--
Phlip http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
Julián Albo wrote: You can use something like:
template <class T> T * NullPtr () { return static_cast <T *> (0); } template <class T> bool isNull (T * ptr) { return ptr == 0; }
You have two typographical errors there. Allow me to fix them:
.... urn static_cast <T *> (NULL); }
.... tr) { return ptr == NULL; }
Aah, much more readable and pronouncable.
--
Phlip http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
gottlobfrege wrote: The prevailing style is that it is a religious debate similar to where you put your brackets, only the case of null, the standard is working on resolving the debate.
Part of learning C++ is learning which style issues are really technical
issues. {} is style. NULL is technical.
--
Phlip http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
Alf P. Steinbach wrote: * red floyd: Footnote 180 to 18.1/4 specifically disallows (void*)0, is there any good modern reason to use 0 instead of NULL anymore?
NULL generally indicates (to the human reader) a pointer value, in about the same way that struct indicates (to the human reader) a POD.
A rather disagree that struct indicates POD. It is an artificial
indicator at best, one easily broken as the language makes no such
distinction. I don't think this is something that can be depended on
at all and therefore is no indicator that we have a POD vs. non-POD.
Phlip wrote: You can use something like:
template <class T> T * NullPtr () { return static_cast <T *> (0); } template <class T> bool isNull (T * ptr) { return ptr == 0; }
You have two typographical errors there. Allow me to fix them:
... urn static_cast <T *> (NULL); } ... tr) { return ptr == NULL; }
Aah, much more readable and pronouncable.
We can make another enhancement:
template <class T> bool isNull (T * ptr) { return isNull (ptr); }
Much more readable this way X-)
--
Salu2
Inviato da X-Privat.Org - Registrazione gratuita http://www.x-privat.org/join.php
In message <Nt************ *******@newssvr 11.news.prodigy .com>, Phlip
<ph******@yahoo .com> writes Julián Albo wrote:
You can use something like:
template <class T> T * NullPtr () { return static_cast <T *> (0); } template <class T> bool isNull (T * ptr) { return ptr == 0; } You have two typographical errors there. Allow me to fix them:
... urn static_cast <T *> (NULL); } ... tr) { return ptr == NULL; }
And now to get the definition of NULL you have to #include some huge
header file in something which is itself going to be included all over
the place.
Aah, much more readable and pronouncable.
Who wants to do either?
--
Richard Herring go**********@gm ail.com wrote: red floyd wrote: 2. I have, and have read TC++PL3e. My point was that because the Standard 18.1/4 specifies "The macro NULL is an implementation-defined null pointer constant in this International Standard (4.10)." Footnote 180 to 18.1/4 specifically disallows (void*)0, is there any good modern reason to use 0 instead of NULL anymore? You are asking why someone would *still* use 0 instead of NULL, as if NULL was somehow better (ie maybe more descriptive).
I would rather ask why someone would *still* use NULL instead of 0? NULL is a macro, 0 is what you are really saying - why hide that with an unnecessary macro?
There are several good reasons.
The best one : It is more human-friendly... You know the intent of the
programmer (assuming that the programmer uses NULL in pointer contexts
only).
But there are other reasons.
For instance, with Borland C++ 5.0 (for x86 CPU), in 16 bits real mode,
the definition of NULL depends on the memory model.
For memory models where pointers are 16 bits large (tiny, small,
medium), NULL is 0 (and int are 16 bits)
For memory models where pointers are 32 bits (compact, large, huge),
NULL is 0L (and long are 32 bits)
On this platform, the representation for the NULL pointer is
all-bits-zero, which is the same representation than the 0 integer
constant (both signed and unsigned).
Using NULL, can avoid bad behaviors in UB contexts.
For instance:
printf("%p", NULL); /* Undefined behavior : But works "as expected"
with Borland C++ */ P.S. macros are evil.
Even if the standard says that NULL is a macro, it can be a "harmless"
macro.
i.e., it can be implemented as:
/* dummy macro definition */
#define NULL NULL
const int NULL=0;
I would like to say that the fact that NULL is not type safe in C++
doesn't mean that it must not be used... Even if the compiler doesn't
make any difference between NULL and 0, programmers do.
I can think of very very old implementations of C, where "char*" and
"int" appeared to be almost semantically equivalent (because the
compiler did very very bad type checking, and dereferencing int was
"accepted" by the compiler).
But, it would be a very bad idea to use one type for the other.
Phlip wrote: Julián Albo wrote:
You can use something like:
template <class T> T * NullPtr () { return static_cast <T *> (0); } template <class T> bool isNull (T * ptr) { return ptr == 0; }
You have two typographical errors there. Allow me to fix them:
... urn static_cast <T *> (NULL); } ... tr) { return ptr == NULL; }
Aah, much more readable and pronouncable.
Chutzpah, man! You make a great point here. The hairs remain unsplit. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Agoston Bejo |
last post by:
I want to enforce such a constraint on a column that would ensure that the
values be all unique, but this wouldn't apply to NULL values. (I.e. there
may be more than one NULL value in the column.)
How can I achieve this?
I suppose I would get the most-hated "table/view is changing,
trigger/function may not see it" error if I tried to write a trigger that
checks the uniqueness of non-null values upon insert/update.
|
by: iStrain |
last post by:
Hiya. I'm _sure_ this is an FAQ, but Googling hasn't produced the
answer in a way I can make sense out of. I know I should get this, but
so far no way...
I'm creating tables and doing queries in Perl, and Nulls have started
to bother me greatly. The first issue is, as far as I understand it, a
column should be NOT NULL if it is necessary (required) data.
Now, if a column doesn't have to be NOT NULL; that is, it's not
_required_ data,...
|
by: Mike MacSween |
last post by:
This as the row source for a combo:
SELECT qryRole.RoleID, qryRole.Role
FROM qryRole
WHERE (((qryRole.RoleID) Not In (SELECT RoleID FROM qryRoleEvent INNER JOIN
qryEvent ON qryRoleEvent.EventID = qryEvent.EventID WHERE ProdID =
Forms!frmProductions!ProdID)))
ORDER BY qryRole.Role;
If there is just one RoleID with a null value in the subquery then the main
|
by: sathyashrayan |
last post by:
The standard confirms that the following initialization of a struct
struct node
{
---
---
}
struct node var = {NULL};
|
by: junky_fellow |
last post by:
Can 0x0 be a valid virtual address in the address space
of an application ?
If it is valid, then the location pointed by a NULL pointer
is also valid and application should not receive "SIGSEGV"
( i am talking of unix machine ) while trying to read that
location.
Then how can i distinguish between a NULL pointer and an invalid
location ?
Is this essential that NULL pointer should not point to any of
the location in the virtual address...
| |
by: Jason Curl |
last post by:
I've been reading this newsgroup for some time and now I am thoroughly
confused over what NULL means.
I've read a NULL pointer is zero (or zero typecast as a void pointer),
others say it's compiler dependent (and that NULL might be anything, but
it is always NULL).
The source snippet is below. The question is:
- When I use calloc to allocate a block of memory, preinitialising it to
zero, is this equivalent (and portable C) to...
|
by: David Sworder |
last post by:
Hi,
I've created a UserControl-derived class called MyUserControl that is
able to persist and subsequently reload its state. It exposes two methods as
follows:
public void Serialize(Stream s);
public void Deserialize(Stream s);
Within the MyUserControl class, there is a field of type MyInnerClass
|
by: yossi.kreinin |
last post by:
Hi!
There is a system where 0x0 is a valid address, but 0xffffffff isn't.
How can null pointers be treated by a compiler (besides the typical
"solution" of still using 0x0 for "null")?
- AFAIK C allows "null pointers" to be represented differently then
"all bits 0". Is this correct?
- AFAIK I can't `#define NULL 0x10000' since `void* p=0;' should work
just like `void* p=NULL'. Is this correct?
|
by: Aaron Morton |
last post by:
I'm working on a IHttpModule that handles the PreSendRequestHeaders event
from the HttpApplication, if the event is raised after EndRequest then
HttpContext.Current is null. If it is raised before EndRequest (by turning
response buffering off) then HttpContext.Current is set.
To repo add the following to Global.asax
protected void Application_PreSendRequestHeaders(object sender, EventArgs e)
{...
|
by: lovecreatesbea... |
last post by:
Do you prefer malloc or calloc?
p = malloc(size);
Which of the following two is right to get same storage same as the
above call?
p = calloc(1, size);
p = calloc(size, 1);
|
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...
| |
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...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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...
|
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...
|
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...
|
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...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |