null pointer

Discussion in 'C Programming' started by cc, Oct 13, 2011.

  1. cc

    cc Guest

    In C, is derefencing a null pointer illegal?
    cc, Oct 13, 2011
    #1
    1. Advertising

  2. cc

    John Gordon Guest

    In <> cc <> 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
    B is for Basil, assaulted by bears
    -- Edward Gorey, "The Gashlycrumb Tinies"
    John Gordon, Oct 13, 2011
    #2
    1. Advertising

  3. cc

    James Kuyper Guest

    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.
    James Kuyper, Oct 13, 2011
    #3
  4. cc

    Nobody Guest

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

    > In C, is derefencing a null pointer illegal?


    It's "undefined behaviour".
    Nobody, Oct 14, 2011
    #4
  5. 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.
    88888 dihedral, Oct 14, 2011
    #5
  6. 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 )
    ()~*~()
    (_)-(_)
    -------------------------------------------------------------------------------
    Kleuskes & Moos, Oct 14, 2011
    #6
  7. cc

    John Bode Guest

    On Oct 13, 12:35 pm, cc <> 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.
    John Bode, Oct 14, 2011
    #7
  8. On 14 oct, 16:05, John Bode <> wrote:
    > > On Oct 13, 12:35 pm, cc <> 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.
    Jean-Christophe, Oct 15, 2011
    #8
  9. On 10/15/11 11:58 AM, Jean-Christophe wrote:
    > On 14 oct, 16:05, John Bode<> wrote:
    >>> On Oct 13, 12:35 pm, cc<> 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).
    Richard Damon, Oct 15, 2011
    #9
  10. cc

    Joe Pfeiffer Guest

    Richard Damon <> writes:

    > On 10/15/11 11:58 AM, Jean-Christophe wrote:
    >> On 14 oct, 16:05, John Bode<> wrote:
    >>>> On Oct 13, 12:35 pm, cc<> 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....
    Joe Pfeiffer, Oct 15, 2011
    #10
  11. 88888 dihedral <> 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?

    --
    Peter
    Peter Nilsson, Oct 16, 2011
    #11
  12. cc

    James Kuyper Guest

    On 10/15/2011 11:58 AM, Jean-Christophe wrote:
    > On 14 oct, 16:05, John Bode <> 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
    James Kuyper, Oct 16, 2011
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    26,491
    Mike Schilling
    Mar 29, 2006
  2. Ken
    Replies:
    69
    Views:
    12,470
    Michael D. Borghardt
    Jul 31, 2004
  3. Ken
    Replies:
    5
    Views:
    2,513
    Victor Bazarov
    Jul 24, 2004
  4. aneuryzma
    Replies:
    3
    Views:
    691
    Jim Langston
    Jun 16, 2008
  5. Christopher
    Replies:
    4
    Views:
    426
    Ruben Safir
    Jul 9, 2011
Loading...

Share This Page