Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

null pointer

 
 
cc
Guest
Posts: n/a
 
      10-13-2011
In C, is derefencing a null pointer illegal?
 
Reply With Quote
 
 
 
 
John Gordon
Guest
Posts: n/a
 
      10-13-2011
In <(E-Mail Removed)> cc <(E-Mail Removed)> writes:

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

--
John Gordon A is for Amy, who fell down the stairs
http://www.velocityreviews.com/forums/(E-Mail Removed) B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

 
Reply With Quote
 
 
 
 
James Kuyper
Guest
Posts: n/a
 
      10-13-2011
On 10/13/2011 01:35 PM, cc wrote:
> 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.
 
Reply With Quote
 
Nobody
Guest
Posts: n/a
 
      10-14-2011
On Thu, 13 Oct 2011 10:35:29 -0700, cc wrote:

> In C, is derefencing a null pointer illegal?


It's "undefined behaviour".

 
Reply With Quote
 
88888 dihedral
Guest
Posts: n/a
 
      10-14-2011
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.



 
Reply With Quote
 
Kleuskes & Moos
Guest
Posts: n/a
 
      10-14-2011
On Fri, 14 Oct 2011 06:26:05 -0700, 88888 dihedral wrote:

> 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 )
()~*~()
(_)-(_)
-------------------------------------------------------------------------------

 
Reply With Quote
 
John Bode
Guest
Posts: n/a
 
      10-14-2011
On Oct 13, 12:35*pm, cc <(E-Mail Removed)> wrote:
> 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.
 
Reply With Quote
 
Jean-Christophe
Guest
Posts: n/a
 
      10-15-2011
On 14 oct, 16:05, John Bode <(E-Mail Removed)> wrote:
> > On Oct 13, 12:35*pm, cc <(E-Mail Removed)> wrote:
> > 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.


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.
 
Reply With Quote
 
Richard Damon
Guest
Posts: n/a
 
      10-15-2011
On 10/15/11 11:58 AM, Jean-Christophe wrote:
> On 14 oct, 16:05, John Bode<(E-Mail Removed)> wrote:
>>> On Oct 13, 12:35 pm, cc<(E-Mail Removed)> wrote:
>>> 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.

>
> 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).
 
Reply With Quote
 
Joe Pfeiffer
Guest
Posts: n/a
 
      10-15-2011
Richard Damon <(E-Mail Removed)> writes:

> On 10/15/11 11:58 AM, Jean-Christophe wrote:
>> On 14 oct, 16:05, John Bode<(E-Mail Removed)> wrote:
>>>> On Oct 13, 12:35 pm, cc<(E-Mail Removed)> wrote:
>>>> 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.

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


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

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?? qazmlp1209@rediffmail.com 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



Advertisments