Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   null pointer (http://www.velocityreviews.com/forums/t754909-null-pointer.html)

cc 10-13-2011 05:35 PM

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

John Gordon 10-13-2011 05:52 PM

Re: null pointer
 
In <599350ed-5842-4e7b-97b7-a2ecb0fabeb1@x20g2000vbl.googlegroups.com> cc <scatnubbs@hotmail.com> 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
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"


James Kuyper 10-13-2011 05:55 PM

Re: null pointer
 
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.

Nobody 10-14-2011 09:26 AM

Re: null pointer
 
On Thu, 13 Oct 2011 10:35:29 -0700, cc wrote:

> In C, is derefencing a null pointer illegal?


It's "undefined behaviour".


88888 dihedral 10-14-2011 01:26 PM

Re: null pointer
 
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.




Kleuskes & Moos 10-14-2011 01:41 PM

Re: null pointer
 
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 )
()~*~()
(_)-(_)
-------------------------------------------------------------------------------


John Bode 10-14-2011 02:05 PM

Re: null pointer
 
On Oct 13, 12:35*pm, cc <scatnu...@hotmail.com> 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.

Jean-Christophe 10-15-2011 03:58 PM

Re: null pointer
 
On 14 oct, 16:05, John Bode <jfbode1...@gmail.com> wrote:
> > On Oct 13, 12:35*pm, cc <scatnu...@hotmail.com> 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.

Richard Damon 10-15-2011 04:47 PM

Re: null pointer
 
On 10/15/11 11:58 AM, Jean-Christophe wrote:
> On 14 oct, 16:05, John Bode<jfbode1...@gmail.com> wrote:
>>> On Oct 13, 12:35 pm, cc<scatnu...@hotmail.com> 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).

Joe Pfeiffer 10-15-2011 09:17 PM

Re: null pointer
 
Richard Damon <news.x.richarddamon@xoxy.net> writes:

> On 10/15/11 11:58 AM, Jean-Christophe wrote:
>> On 14 oct, 16:05, John Bode<jfbode1...@gmail.com> wrote:
>>>> On Oct 13, 12:35 pm, cc<scatnu...@hotmail.com> 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....


All times are GMT. The time now is 08:53 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.