null pointer

J

John Gordon

In said:
In C, is derefencing a null pointer illegal?

In everyday usage, yes. You're telling the program to look in a location
that does not exist, which is a nonsense operation.

(The C specification might not use the specific term "illegal"; I'm not
a language lawyer.)
 
J

James Kuyper

In C, is derefencing a null pointer illegal?

Nothing in C is illegal, per se. However, the behavior of a program that
dereferences a null pointer is not defined by the C standard. Unless
something else defines that behavior (and probably even if something
else does define it), that's something you'll usually want to avoid.
 
8

88888 dihedral

C is a typed language that supports pointers(*) to even
the void type and a function with variable arguments in various types and lengths.

I think C is not good for beginners without HW knowledge to mess around with pointers and boundaries of arrays not checked in loops
or even in any access instances.
 
K

Kleuskes & Moos

C is a typed language that supports pointers(*) to even the void type
and a function with variable arguments in various types and lengths.

I think C is not good for beginners without HW knowledge to mess around
with pointers and boundaries of arrays not checked in loops or even in
any access instances.

"C is quirky, flawed, and an enormous success." Dennis Ritchie

-------------------------------------------------------------------------------
________________________________________
/ First, I'm going to give you all the \
| ANSWERS to today's test ... So just |
\ plug in your SONY WALKMANS and relax!! /
----------------------------------------
\
\
___
{~._.~}
( Y )
()~*~()
(_)-(_)
-------------------------------------------------------------------------------
 
J

John Bode

In C, is derefencing a null pointer illegal?

The behavior is "undefined" - any result is possible. On platforms
such as Windows or *nix, you'll most likely get a segfault.

It's definitely a logic error, since NULL represents a well-defined
"nowhere"; there shouldn't *be* anything there.
 
J

Jean-Christophe

The behavior is "undefined" - any result is possible.  On platforms
such as Windows or *nix, you'll most likely get a segfault.
It's definitely a logic error, since NULL represents a well-defined
"nowhere"; there shouldn't *be* anything there.

Of course there is something at adress zero :
on a home-made uP electronic board one can map
a RAM chip at address zero and read/write here.

C compilers prohibits the dereference
of zero pointer just to check for errors.
 
R

Richard Damon

Of course there is something at adress zero :
on a home-made uP electronic board one can map
a RAM chip at address zero and read/write here.

C compilers prohibits the dereference
of zero pointer just to check for errors.

Undefined behavior may do something useful. On many machines the memory
pointed to by a null pointer is not in the user addressable space, and a
trap will occur on access. On others, you may not get a trap, but it
will access memory that has special purposes.

The C implementation is required to make sure that the address of every
object created is distinct from the null pointer.

Note also that a 'Null Pointer" is not required to be "Address 0", one
way to create it is to assign a pointer variable with a constant
expression with value 0, but that doesn't mean that it itself is "0"
(though it tends to be).
 
J

Joe Pfeiffer

Richard Damon said:
Undefined behavior may do something useful. On many machines the
memory pointed to by a null pointer is not in the user addressable
space, and a trap will occur on access. On others, you may not get a
trap, but it will access memory that has special purposes.

For years I honestly thought a null char* was a valid representation of
a 0-length string. You see, on a VAX, address 0 was readable, and (for
reasons I may have known once) always contained a 0. I long ago forgot
how much code needed to be fixed when I moved it all to Suns....
 
P

Peter Nilsson

88888 dihedral said:
I think  C is not good for beginners without HW knowledge ...

I think C is not good for beginners *with* harware knowledge.
By far the biggest mistake most new C programmers make is thinking
of every piece of syntax in terms of cpu instructions. It comes as
a great shock to many when they realise (or are told) that C really
isn't a portable assembler.

I wonder if C had better string handling abilities, would it still
be thought of as such a 'low' high level language?
 
J

James Kuyper

Of course there is something at adress zero :
on a home-made uP electronic board one can map
a RAM chip at address zero and read/write here.

Address 0 and a null pointer need not have anything to do with each
other. If it's useful to read or write from address 0, then a null
pointer should not be implemented as a pointer to the memory at address
0, because the C standard prohibits null pointers from comparing equal
to pointers pointing at actual objects or functions.

There's also no inherent reason why address 0 has to even be meaningful.
Valid addresses might be restricted to the range 0x10000000 to 0xFFFFFFFF.
C compilers prohibits the dereference
of zero pointer just to check for errors.

No, it prohibits the dereference because it doesn't make logical sense.
Null pointers say "I'm not pointing at anything". Dereferencing a
pointer means "access the object you're pointing at", which is a
ridiculous thing to ask for when it isn't actually pointing at anything.
As a result, on many platforms, attempting to do it can cause your
program to malfunction or abort. This is implemented at the hardware or
operating system level, making it very inefficient for a compiler to
generate the code needed to bypass that behavior. Making the behavior
undefined gives implementors the freedom they need to generate code for
which this is a possible failure mode.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top