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? 51 3193
And what if I dereference a pointer to a structure like this:
SturctPtr->Member;
What should happen if StructPtr is NULL and Member is not first in the
structure, i.e. it is alligned at 4 bytes from the beginning?
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?
I know that in C++ the use of NULL is almost deprecated.
Stroustrup suggests to prefer 0, because no object is allocated at the
address 0.
Moreover, I don't think that exists a standard behaviour about Null
pointer deferencing.
It's something that happens at run time and a C++ compiler has no
control on it (IMHO).
P.S:
Why on the Earth a person should try to dereference (with awareness) a
NULL pointer ???
Bye
Andrea
BigMan wrote: And what if I dereference a pointer to a structure like this:
SturctPtr->Member;
What should happen if StructPtr is NULL and Member is not first in the structure, i.e. it is alligned at 4 bytes from the beginning?
The result of dereferencing a null pointer is _undefined_. IOW, anything
can happen and is allowed to happen AFA C++ is concerned. Most modern
operating systems will indicate some kind of exceptional situation and
abort the offending program. But that's not guaranteed.
V
BigMan wrote: Does the C++ standard define what should happen in case of NULL
pointer dereferencing.
Yes.
It says what happens is undefined.
On Tue, 15 Feb 2005 15:09:27 GMT, Andrea Sini <do@notwrite.it > wrote: 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? I know that in C++ the use of NULL is almost deprecated.
Stroustrup suggests to prefer 0, because no object is allocated at the address 0.
He's wrong (if he actually suggests it for that reason). The constant
zero assigned or compared to a pointer is evaluated as a null pointer,
whatever value that has in that implementation (it might actually be all
bits set, or a 'trap' value). An implementation can put objects at an
address of zero if it likes.
Moreover, I don't think that exists a standard behaviour about Null pointer deferencing.
It's explicitly undefined behaviour, and can cause demons to fly out of
your nose, or World War III, or an asteroid to hit your home town. It
may cause a trap (bus error, interrupt, signal, code dump or whatever)
but it may not (and will not on one of the most common CPU architectures
as well as on some systems under another common architecture).
(As for what happens when one dereferences a derived pointer, like a
structure member, that's even less defined if that's possible, because
many systems won't even trap it...)
It's something that happens at run time and a C++ compiler has no control on it (IMHO).
Well, a compiler could issue code protecting every memory access...
Why on the Earth a person should try to dereference (with awareness) a NULL pointer ???
Perhap the OP likes demons flying out of zir nose? I haven't tried it
myself, it might be a turn-on for some people (like banging heads
against a brick wall).
Chris C
#include <stdio.h>
class MyClass {
public:
void A() {
if (this==NULL) printf("NULL Pointer\n");
}
};
void main() {
MyClass *ptr = 0;
ptr->A();
}
Chris Croughton wrote: On Tue, 15 Feb 2005 15:09:27 GMT, Andrea Sini <do@notwrite.it > wrote:
BigMan wrote:
Does the C++ standard define what should happen in case of NULL pointer dereferencin g. If not, does it say that it is illegal? I know that in C++ the use of NULL is almost deprecated.
Stroustrup suggests to prefer 0, because no object is allocated at the address 0.
He's wrong (if he actually suggests it for that reason).
He actually suggests it for a different reason. 'NULL' is a simple macro
that in most implementations (all implementations I've seen) expands into
0. So, by using 'NULL' instead of '0' the programmer is fooling himself
into believing he's using a pointer while really it's just a damn integer
literal. Using '0' instead of a symbol [mis-]representing it is clearer.
The constant zero assigned or compared to a pointer is evaluated as a null pointer, whatever value that has in that implementation (it might actually be all bits set, or a 'trap' value). An implementation can put objects at an address of zero if it likes.
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. 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.
[...]
V
ajitho wrote: #include <stdio.h>
class MyClass { public: void A() { if (this==NULL) printf("NULL Pointer\n"); } };
void main() { MyClass *ptr = 0; ptr->A(); }
What is your point? Do you not understand the word "undefined" ?
Your program has at least two reasons for undefined behaviour. The
first is declaring 'main' "void".
V
#include <stdio.h>
class MyClass {
public:
void A(const MyClass &arg)
{
if (&arg == 0) {
printf("Null reference!\n");
}
}
};
void main() {
MyClass ptr;
ptr.A( *(MyClass *)0);
} This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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: Mikhail Teterin |
last post by:
Hello!
Consider the following simple accessor function:
typedef struct {
int i;
char name;
} MY_TYPE;
const char *
|
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};
|
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)
{
|
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;
| |
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.
|
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() {
|
by: prashant.khade1623 |
last post by:
I am not getting the exact idea.
Can you please explain me with an example.
Thanks
|
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
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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: 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: 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();...
|
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: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |