Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > null pointer

Thread Tools

null pointer

Peter Nilsson
Posts: n/a
88888 dihedral <(E-Mail Removed)> wrote:
> 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?

Reply With Quote
James Kuyper
Posts: n/a
On 10/15/2011 11:58 AM, Jean-Christophe wrote:
> On 14 oct, 16:05, John Bode <(E-Mail Removed)> wrote:

>> 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.

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.
James Kuyper
Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
pointer to pointer intialize to NULL but still point to NULL Christopher C++ 4 07-09-2011 12:35 AM
Null pointer (NULL array pointer is passed) aneuryzma C++ 3 06-16-2008 05:48 AM
"stringObj == null" vs "stringObj.equals(null)", for null check?? Java 5 03-29-2006 10:37 PM
NULL macro vs. 0 as null pointer? Ken C++ 69 07-31-2004 04:45 AM
Followup: NULL macro vs. 0 as null pointer? Ken C++ 5 07-24-2004 04:05 PM