473,791 Members | 2,711 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Null references.

Is it OK to dereference NULL pointers if the only thing you are doing
is storing them in a reference then comparing addresses to NULL again
later, as in:

=== BEGIN EXAMPLE ===

Something g_something;

const Something & getSomething () {
if (condition)
return g_something;
else
return *(const Something *)NULL; // <--- !!!
}

void function () {
const Something &s = getSomething;
if (&s == NULL) { // <--- !!!
// ...
} else {
// ...
}
}

=== END EXAMPLE ===

Thanks,
Jason
Jul 16 '08 #1
6 1468
Sam
ja************@ gmail.com writes:
Is it OK to dereference NULL pointers if the only thing you are doing
is storing them in a reference then comparing addresses to NULL again
later, as in:
Well, it or may not be legal. But, doing that is like dressing up like a
Santa Claus and walking past a hungry bull. It's not advisable.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEABECAAYFAkh 9WuoACgkQx9p3GY HlUOL0pwCfc7HdL KZK9k5Y3kv4zHvg APC/
4AQAnRX+YJPQTD9 kr3sgONXqjCwBka cQ
=Ykj2
-----END PGP SIGNATURE-----

Jul 16 '08 #2
On Jul 15, 10:32 pm, "Alf P. Steinbach" <al...@start.no wrote:
* jason.cipri...@ gmail.com:
Is it OK to dereference NULL pointers if the only thing you are doing
is storing them in a reference then comparing addresses to NULL again
later

No.

That incurs Undefined Behavior.
Then, if this is undefined:

Something &ref = *(Something &)NULL;

Doesn't that mean this also undefined?

Something *ptr = NULL;
Something &ref = *ptr;

And if so, then that also means this is undefined, as the possibility
exists for the pointer to be NULL?

Something * function ();
Something *ptr = function();
Something &ref = *ptr;

And if that is undefined, is this the cleanest way to make sure
nothing undefined happens?

Something * function ();
Something *ptr = function();
if (ptr) {
Something &ref = *ptr;
// ...
} else {
// error
}

Also, unrelated to references, just out of curiosity, does that mean
statements like this are also undefined:

Something *ptr = &(*(Somethin g *)NULL);

Thanks,
Jason
Jul 16 '08 #3
On Jul 15, 10:49 pm, "jason.cipri... @gmail.com"
<jason.cipri... @gmail.comwrote :
Then, if this is undefined:

Something &ref = *(Something &)NULL;
I'm sorry this should read:

Something &ref = *(Something *)NULL;

Jason
Jul 16 '08 #4
On Jul 15, 11:04 pm, "Alf P. Steinbach" <al...@start.no wrote:
Depends on whether the pointer actually can be 0. It's the actual dereferencing
of a nullpointer that incurs Undefined Behavior. Not the potential.
Oh, I guess that makes sense.

Thanks for the good response and explanation.

Jason
Jul 16 '08 #5
On Jul 16, 5:04 am, "Alf P. Steinbach" <al...@start.no wrote:
There are some exceptions to incurring UB for dereferencing a
nullpointer. The most important one is that any given compiler
vendor can decide to give that a well-defined meaning at
compile time for that compiler, and exploit that to implement
various macros in the standard library. Another one is the
context of a typeid call.
Hows that?

Modulo vendor specific guarantees (of course), it's undefined
behavior to dereference a null pointer in code that actually
gets executed. Something like sizeof( *p ) is fine, even if p
is a null pointer. But typeid normally requires execution,
e.g.: typeid( *p ) is undefined behavior if p is a null pointer
(and the pointed to object has a polymorphic type, but then,
that's the usual reason for using typeid).

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jul 16 '08 #6
On Jul 16, 1:28 pm, "Alf P. Steinbach" <al...@start.no wrote:
* James Kanze:
On Jul 16, 5:04 am, "Alf P. Steinbach" <al...@start.no wrote:
There are some exceptions to incurring UB for dereferencing a
nullpointer. The most important one is that any given compiler
vendor can decide to give that a well-defined meaning at
compile time for that compiler, and exploit that to implement
various macros in the standard library. Another one is the
context of a typeid call.
Hows that?
Modulo vendor specific guarantees (of course), it's undefined
behavior to dereference a null pointer in code that actually
gets executed. Something like sizeof( *p ) is fine, even if p
is a null pointer. But typeid normally requires execution,
e.g.: typeid( *p ) is undefined behavior if p is a null pointer
(and the pointed to object has a polymorphic type, but then,
that's the usual reason for using typeid).
I'm sorry, that's incorrect. §5.2.8/2, typeid guarantees to
throw a std::bad_typeid exception if you give it an lvalue
formed by dereferencing a nullpointer to polymorphic type. So
we're out of UB-land and into guaranteed exception-land. :-)
Interesting. A bit strange, but who's to complain. Anything
which reduces undefined behavior is good.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jul 16 '08 #7

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

Similar topics

10
42423
by: Bodza Bodza | last post by:
I'm having an argument with an incumbent self-taught programmer that it is OK to use null foreign keys in database design. My take is the whole point of a foreign key is that it's not supposed to be optional, it's very definition is it's a necessary link to the parent table and part of the definition. If it's optional it shouldn't be part of the definition of a table and should be in a linking table instead. Comments?
99
5200
by: Mikhail Teterin | last post by:
Hello! Consider the following simple accessor function: typedef struct { int i; char name; } MY_TYPE; const char *
10
2606
by: Jason Collins | last post by:
There are some Requests that occur that have a NULL UserAgent. Some of these are legitimate crawlers (e.g., Overture's crawler), and I'm attempting to adjust my <browserCaps> to correctly classify crawlers. In an attempt to capture browsers with (UserAgent == null), I've added a case like this: <filter> <case match="^$"> crawler=true
7
2215
by: teddysnips | last post by:
Table DDL below: The tables I have contain Timesheet information. Each row in the tblTSCollected table contains an entry for an employee into the timesheet system, specifically by scanning the barcode on their badge. A whole bunch of business logic periodically attempts to "pair" these into logically matched scans. For example, some employees will scan in and out of a single place of work. For these there will be a row written to...
3
1708
by: Vincent RICHOMME | last post by:
Hi, I would like to know how I can return a NULL reference. usually when I need to return an object and to know if the object is valid I use a pointer (if the object is nto valid I return NULL) : MyObj* MyClass::GetObj(const wxString& strObjName ) { ReaderIt it = m_scObjs.find( strObjName );
12
16773
by: Joe | last post by:
I might be overworked so please excuse this stupid question... Say I do the following: DataTable table = new DataTable(); myDataAdaptor.Fill(table); dataGrid1.DataSource = table;
27
4234
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) {
11
3268
by: MikeT | last post by:
This may sound very elementary, but can you trap when your object is set to null within the object? I have created a class that registers an event from an object passed in the constructor. When my object is destroyed, I want my object to un-register this event. If I don't then the object would never be destroyed until the object I passed in the constructor is destroyed. I have implemented a Dispose(), Dispose(bool), and ~Finalize...
76
4720
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.
8
2302
by: A. Anderson | last post by:
Howdy everyone, I'm experiencing a problem with a program that I'm developing. Take a look at this stack report from GDB - #0 0xb7d782a3 in strlen () from /lib/tls/i686/cmov/libc.so.6 #1 0xb7d4c2f7 in vfprintf () from /lib/tls/i686/cmov/libc.so.6 #2 0xb7d6441b in vsprintf () from /lib/tls/i686/cmov/libc.so.6 #3 0x08049ba0 in character_data::printf (this=0x800, argument=0x0) at character.c:198
0
9669
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
10207
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...
1
10154
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,...
0
9029
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
7537
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
6776
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
5430
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...
2
3713
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2913
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.