473,405 Members | 2,210 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

NULL pointer dereferencing?

Does the standard define what happens when a NULL pointer is dereferenced? If so, where?
Mar 2 '06 #1
5 5079
no

Mar 2 '06 #2
Angel Tsankov wrote:
Does the standard define what happens when a NULL pointer is
dereferenced? If so, where?


That's probably the classic case of undefined behaviour.

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Mar 2 '06 #3
Angel Tsankov wrote:
Does the standard define what happens when a NULL pointer is
dereferenced? If so, where?


It is in the definition of undefined behavior as a matter of
fact.
Mar 3 '06 #4
Angel Tsankov wrote:
Does the standard define what happens when a NULL pointer is dereferenced? If so, where?


In some cases the behavior is defined, namely where the expression is
not actually evaluated.

#include <cstdlib>

// ...

type *p = (type *) malloc(sizeof *p);

Here p is dereferenced, and doesn't even have a value yet, let alone a
null one. That's okay, because the sizeof operator only needs the
expression for its type, not its value. The expression is not
evaluated.

By the way, don't call it a NULL pointer. There are null pointers,
null characters, the ASCII character NUL and the NULL constant.
Typography matters!

:)

There are some shady uses of null pointers which are strictly speaking
undefined, but are quasi-defined in the sense of having quite broad
portability.

You may find a use of a null pointer in the definition of the
offsetof() macro from ANSI C.

This is from the comp.lang.c FAQ:

9.8: How can I determine the byte offset of a field
within a structure?

ANSI C defines the offsetof macro, which should be used if available;
see <stddef.h>. If you don't have it, a suggested implementation is
#define offsetof(type, mem) ((size_t) \
((char *)&((type *) 0)->mem - (char *)((type *) 0)))

This implementation is not 100% portable; some compilers may
legitimately refuse to accept it.

As you can see, there is a dereference of a null pointer in there,
namely the ((type *) 0)->mem! The value is not used: rather, the
address of that dereferenced value is taken right away. The address is
subtracted from the null pointer, to make the construct portable to
systems where a null pointer does not convert to an integer zero, and
the result of the subtraction is the offset of that member within the
struct.

Pointer arithmetic on null pointers is also undefined.

Mar 3 '06 #5
Angel Tsankov wrote:
Does the standard define what happens when a NULL pointer is dereferenced? If so, where?


The Standard leaves the behavior undefined in this case.

However, nearly every implementation does define the behavior (which an
implementation is free to do) - usually as a memory access violation.

Greg

Mar 3 '06 #6

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

Similar topics

51
by: BigMan | last post by:
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?
10
by: Denis Palmeiro | last post by:
Hello eveyone. What exactly is a NULL Pointer Dereference? I tried searching google but the only results that returned were bug reports. Some example code, if possible, would also be...
102
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"...
29
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...
99
by: Mikhail Teterin | last post by:
Hello! Consider the following simple accessor function: typedef struct { int i; char name; } MY_TYPE; const char *
27
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) {
51
by: atv | last post by:
Hi, Just to check, if i set a pointer explicitly to NULL, i'm not allowed to dereference it? Why is that, it's not like it's pointing to any garbage right? Why else set it to NULL. I can remember...
7
by: Lutz Richter | last post by:
Hi, given the following code: ----------------------------- class B { public: B(): Value(99) {} int Get() { if (this) return Value; else return -1; }
76
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...
20
by: prashant.khade1623 | last post by:
I am not getting the exact idea. Can you please explain me with an example. Thanks
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
Oralloy
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,...
0
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...
0
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,...
0
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...

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.