473,804 Members | 2,100 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

NULL pointer dereferencing - behaviour

Does the C++ standard define what should happen in case of NULL pointer
dereferencing. If not, does it say that it is illegal?

Where, if so, does it say it?

Jul 23 '05
51 3196
ajitho wrote:
...
Look at the definition of the "offsetof" macro in whatever C/C++
compiler you use.
So? The implementation of the standard library is not limited by the
bounds of correct C++ language. Furthermore, there is absolutely no
requirement that the standard library is implemented in C++. It could be
implemented in assembly language or in Fortran, for example. You saw an
implementation that dereferences a null pointer? The only thing that
follows from that is that the library that you saw is not implemented in
C++. It is implemented in some language that probably [very] closely
resembles C++ (that's why you got confused by it), but is not C++.
Therefore, your example with 'offsetof' is not relevant.
It is obviously dereferencing a null pointer. It is not making use of
the <b>value</b> of the dereference. There is a suble difference.


Sorry, but C++ language's idea of "use" (of the value of the
dereference) is different from yours.

--
Best regards,
Andrey Tarasevich
Jul 23 '05 #21
BigMan wrote:
Does the C++ standard define what should happen in case of NULL pointer
dereferencing. If not, does it say that it is illegal?

Where, if so, does it say it?


In the current version of the standard dereferencing null pointer leads
to undefined behavior. Although there some places in the standard that
seem to be worded incorrectly, which may lead the reader to believe that
in some situations dereferencing null pointer is OK (see for example,
the quite from 'typeof' specification in
http://www.comeaucomputing.com/iso/cwg_active.html#232).

However, the existing developments in the area of C++ specification
(once again, see DR#232) seem to suggest that in the future
dereferencing of a null pointer will be allowed as long as the resultant
lvalue is not used as (converted to) an rvalue. I.e. the behavior of
following code

int* p = NULL;
int* q = &*p;

, which is undefined in the current language specification, will
probably become defined in the future.

--
Best regards,
Andrey Tarasevich
Jul 23 '05 #22
Noah Roberts wrote:

BigMan wrote:
Does the C++ standard define what should happen in case of NULL pointer dereferencing.

Yes.


Actually, no.
It says what happens is undefined.


Yes, so IOW, it doesn't define what happens.

Jul 23 '05 #23
Victor Bazarov wrote:
The introduction of a null pointer constant represented by a reserved word
("nullptr") will hopefully resolve all the confusion caused by "should I
use NULL or 0 for pointers", since it will make a clear distinction
between the two.
That sounds interesting. It would virtually put to rest what's probably the
most popular topic in C++ discussion forums (for better or worse).

Of the two (NULL or 0), I've always used 0? Why? It's portable! No matter
what compiler I use or what header file inclusion combo I use, IT ALWAYS
COMPILES!

The ancient code I'm working on now has lots of NULL "macros" defined in
many, many places (blecchh). Whenever I come across a module littered with
these nasty things, I zap them with a global replace in emacs to 0.

There's one advantage I see to NULL. Doing searches for "NULL" will yield
more relevant results than searches for 0 obviously. However, I believe
that portability is a better trade off.
The conversion will still exist (since the introduction
cannot break zillions of tons of code already written to use 0), but many
good things are going to come out of it. You can read more about it in
the document SC22/WG21/N1601 J16/04-0041.


Speaking of backward compatibility, I'm also dealing with test statements of
checking for a null pointer after executing a new by inserting the
(nothrow) within the operation.

Keith

Jul 23 '05 #24
Keith P. Boruff wrote:
...
Of the two (NULL or 0), I've always used 0? Why? It's portable! No matter
what compiler I use or what header file inclusion combo I use, IT ALWAYS
COMPILES!
'NULL' is as portable as '0' in the formal meaning of the term
"portable". The only thing you gain by using '0' is that you don't have
to include the corresponding header file. But frankly I don't see any
benefit in that.
The ancient code I'm working on now has lots of NULL "macros" defined in
many, many places (blecchh). Whenever I come across a module littered with
these nasty things, I zap them with a global replace in emacs to 0.
That's the problem of the code. User code is not supposed to define it's
own 'NULL' macro. It is supposed to use the definition provided by the
standard library. I don't know why you chose to replace 'NULL's with
'0's instead of simply removing the user's definitions and including the
proper header instead.
There's one advantage I see to NULL. Doing searches for "NULL" will yield
more relevant results than searches for 0 obviously. However, I believe
that portability is a better trade off.


I find your idea of "portabilit y" to be rather strange.

--
Best regards,
Andrey Tarasevich
Jul 23 '05 #25
Andrey Tarasevich wrote:


That's the problem of the code. User code is not supposed to define it's
own 'NULL' macro. It is supposed to use the definition provided by the
standard library. I don't know why you chose to replace 'NULL's with
'0's instead of simply removing the user's definitions and including the
proper header instead.
I do remove the NULL definition along with the NULLs. And.. I don't add a
header file because adding a header file for one stupid constant causes
unneeded coupling.
There's one advantage I see to NULL. Doing searches for "NULL" will yield
more relevant results than searches for 0 obviously. However, I believe
that portability is a better trade off.


I find your idea of "portabilit y" to be rather strange.


Shrug.

Adding an entire header file just for NULL? That isn't strange?
Jul 23 '05 #26

"ajitho" <aj************ @yahoo.com> skrev i en meddelelse
news:11******** **************@ f14g2000cwb.goo glegroups.com.. .
I'm sure you know much more about the language than me. I thought the
"grey area" was legal .... (Falling back on the reasoning that the
dereferenced data is not actually accessed -- we're just playing around
with its address).
You're probably right ....

You are always allowed to write code with undefined behaviour, but in this
case you trust a particular environment/compiler to give you some guarantees
not part of the standard. The bottomline is that it is still undefined
behaviour and thus not portable. This is especially troublesome in case you
might consider using your code with different compilers - or just upgrading
your compiler. Those who write the standard library for such a compiler have
a greater liberty than the rest of us.

/Peter
Jul 23 '05 #27
Keith P. Boruff wrote:

That's the problem of the code. User code is not supposed to define it's
own 'NULL' macro. It is supposed to use the definition provided by the
standard library. I don't know why you chose to replace 'NULL's with
'0's instead of simply removing the user's definitions and including the
proper header instead.


I do remove the NULL definition along with the NULLs. And.. I don't add a
header file because adding a header file for one stupid constant causes
unneeded coupling.
...
Adding an entire header file just for NULL? That isn't strange?


That's just how things are done in C++. If you want to make sure that
things that have to be defined identically in different translation
units are indeed defined identically in different translation units, you
put the definitions in the header file and #include it wherever it is
needed. How much stuff you heave defined in that header file is not
really relevant.

Besides, I don't really see how the inclusion of a standard library
header file creates "unneeded coupling".

--
Best regards,
Andrey Tarasevich
Jul 23 '05 #28
Andrey Tarasevich wrote:
Adding an entire header file just for NULL? That isn't strange?
That's just how things are done in C++. If you want to make sure that
things that have to be defined identically in different translation
units are indeed defined identically in different translation units, you
put the definitions in the header file and #include it wherever it is
needed. How much stuff you heave defined in that header file is not
really relevant.


I'm working on a project that includes an excess of 500 files. Are you
saying that I should include the proper header file in each of these (or
most) just for the benefit of NULL? Nope! Take out the old header file with
the homegrown defined NULL, change the NULL references to 0. End of story.

Besides, I don't really see how the inclusion of a standard library
header file creates "unneeded coupling".


If I were to go ahead and add this std header file in all the applicalble
modules on my project, I'd quickly piss off a lot of people on my team
because they'd have to do needless re-builds... and for what? NULL?

Granted, some rebuilds have been necessary as a result of me zapping NULL
but it's a one time thing then it's done.

This is an old, old argument. I'm not going to convince you and you're not
going to convince me. All I can say is that if I ever work from you, I'd
have to use NULL. If you worked for me, you'd have to use 0.

Keith
Jul 23 '05 #29
Keith P. Boruff wrote:
...
That's just how things are done in C++. If you want to make sure that
things that have to be defined identically in different translation
units are indeed defined identically in different translation units, you
put the definitions in the header file and #include it wherever it is
needed. How much stuff you heave defined in that header file is not
really relevant.
I'm working on a project that includes an excess of 500 files. Are you
saying that I should include the proper header file in each of these (or
most) just for the benefit of NULL? Nope! Take out the old header file with
the homegrown defined NULL, change the NULL references to 0. End of story.
...


Firstly, I don't really see the difference between 'NULL' and any other
component of the standard library. You have to include the proper header
file to use 'std::vector', 'malloc' or 'size_t', don't you? These
entities belong to rather generic level, which means that they will be
'#include'd virtually everywhere. How is that different from including
the proper header file for 'NULL'?

Secondly, there's no dedicated header file "just for NULL" in the
standard library. All header files that define 'NULL' also define
something else, like 'size_t' and 'ptrdiff_t'. Do you also avoid using
'size_t' for the same reason?

Besides, I don't really see how the inclusion of a standard library
header file creates "unneeded coupling".


If I were to go ahead and add this std header file in all the applicalble
modules on my project, I'd quickly piss off a lot of people on my team
because they'd have to do needless re-builds... and for what? NULL?

Granted, some rebuilds have been necessary as a result of me zapping NULL
but it's a one time thing then it's done.


Exactly as it is with inclusion of the header file for 'NULL'! "It is
one time thing and then it's done". Once again, I don't see the difference.
This is an old, old argument. I'm not going to convince you and you're not
going to convince me. All I can say is that if I ever work from you, I'd
have to use NULL. If you worked for me, you'd have to use 0.


I'm just trying to find out whether your position is dogmatic or based
on some kind of logic. So far it appears to be purely dogmatic. Under
these circumstances I don't expect to convince you in anything.

--
Best regards,
Andrey Tarasevich
Jul 23 '05 #30

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

Similar topics

29
3772
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...
99
5220
by: Mikhail Teterin | last post by:
Hello! Consider the following simple accessor function: typedef struct { int i; char name; } MY_TYPE; const char *
204
13142
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 = {0,1,2,4,9};
27
4240
by: David W | last post by:
I'm almost tearing my hair out. A colleague claimed that a null reference can exist, like this: void f( int& p ) { printf( "%d\n", p ); } int main (int argc, char *argv) {
18
2981
by: Denis Petronenko | last post by:
Hello, in the following code i have segmentaion fault instead of exception. Why? What i must to do to catch exceptions in such situation? Used compiler: gcc version 3.3.6 (Debian 1:3.3.6-13) int main() { try{ int* p = NULL; *p = 4;
76
4729
by: valentin tihomirov | last post by:
As explained in "Using pointers vs. references" http://groups.google.ee/group/borland.public.delphi.objectpascal/browse_thread/thread/683c30f161fc1e9c/ab294c7b02e8faca#ab294c7b02e8faca , the pointers are allowed to be null, while references must refer an existing variable of required type. The null is normally used for making optional parameters. But there is no way to pass null reference in C#. Something is missing.
11
1555
by: Alan Woodland | last post by:
Hi, I'm fairly sure this is undefined behaviour, despite the fact that it compiles and 'runs' (prints "this doesn't exist") on all my platforms: #include <iostream> class foo { public: void bar() {
20
3238
by: prashant.khade1623 | last post by:
I am not getting the exact idea. Can you please explain me with an example. Thanks
28
1879
by: rahul | last post by:
#include <stdio.h> int main (void) { char *p = NULL; printf ("%c\n", *p); return 0; } This snippet prints 0(compiled with DJGPP on Win XP). Visual C++ 6.0
0
9712
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
10343
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
10089
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
9171
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...
0
6862
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
5530
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
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4308
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
2
3831
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.