NULL Pointer Dereference

Discussion in 'C Programming' started by Denis Palmeiro, Jul 8, 2003.

  1. Hello eveyone.

    What exactly is a NULL Pointer Dereference? I tried searching google but
    the only results that returned were bug reports. Some example code, if
    possible, would also be appreciated.

    Thanks in advance.


    -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
    http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
    -----== Over 80,000 Newsgroups - 16 Different Servers! =-----
    Denis Palmeiro, Jul 8, 2003
    #1
    1. Advertising

  2. Denis Palmeiro

    Jun Woong Guest

    "Denis Palmeiro" <> wrote in message news:p...
    > Hello eveyone.
    >
    > What exactly is a NULL Pointer Dereference? I tried searching google but
    > the only results that returned were bug reports. Some example code, if
    > possible, would also be appreciated.
    >


    int *p = 0;
    *p; // dereferencing a null pointer


    --
    Jun, Woong ()
    Dept. of Physics, Univ. of Seoul
    Jun Woong, Jul 8, 2003
    #2
    1. Advertising

  3. Denis Palmeiro

    Guest

    Yes, that's how we dereferenc pointer. Dereferencing them, yields tha
    actual data they point to.

    Generally, NULL is considered to be a error condition. Like the
    pointer is not pointing anywhere.

    To declare NULL pointer use,

    int * intPtr = (int *) NULL; /* NULL is defined as 0 */

    Dereferncing NULL pointer may cause illegal operation...I am not sure

    sumit

    "Jun Woong" <> wrote in message news:<bedhmv$mjm$>...
    > "Denis Palmeiro" <> wrote in message news:p...
    > > Hello eveyone.
    > >
    > > What exactly is a NULL Pointer Dereference? I tried searching google but
    > > the only results that returned were bug reports. Some example code, if
    > > possible, would also be appreciated.
    > >

    >
    > int *p = 0;
    > *p; // dereferencing a null pointer
    , Jul 8, 2003
    #3
  4. On 8 Jul 2003 07:13:19 -0700, wrote:

    >Yes, that's how we dereferenc pointer. Dereferencing them, yields tha
    >actual data they point to.
    >
    >Generally, NULL is considered to be a error condition. Like the
    >pointer is not pointing anywhere.
    >
    >To declare NULL pointer use,
    >
    >int * intPtr = (int *) NULL; /* NULL is defined as 0 */
    >
    >Dereferncing NULL pointer may cause illegal operation...I am not sure
    >
    >sumit


    Please do not top post. I actually thought that Jun's post was a
    correct answer. Your post has some odd constructions. For example,
    you do not need to cast NULL explicitly.

    NULL is a macro defined in stddef.h. to be the null pointer constant.
    A null pointer constant is an integer constant expression with the
    value 0 or such an expression casted to void*. A null pointer
    constant converted to a pointer type is called a null pointer.

    #include <stddef.h>

    int* ip = NULL;
    *ip; // undefined behavior

    would be sufficient. The problem with derefencing a null pointer is
    that it invokes undefined behavior. It is impossible to know how any
    particular implementation will handle it.

    Best wishes,

    Bob
    Robert W Hand, Jul 8, 2003
    #4
  5. Denis Palmeiro

    prashna Guest

    If the pointer is not initialised expicitly, does'nt it get
    initialised to NULL by default.I have written the following program
    which shows that by default any pointer will be intialised to
    NULL.Have a look at the program.

    #include <stdio.h>

    int main()
    {
    int *p;
    if (p ==NULL)
    printf("pointer is null %p\n",p);
    else
    printf("pointer is not null\n");

    }

    and here is the output,
    $ cc dereference_null.c
    $ ./a.out
    pointer is null 0

    or does it depends on the system that is being used?I executed this
    program on AIX.
    prashna, Jul 9, 2003
    #5
  6. Denis Palmeiro

    Mr. 4X Guest

    Denis Palmeiro <> wrote:

    > Hello eveyone.
    >
    > What exactly is a NULL Pointer Dereference? I tried searching google but
    > the only results that returned were bug reports.


    No wonder - dereferencing a NULL po9inter *is* usually a bug. If you try to
    READ the stuff to which the null pointer points then you may get away with
    garbage values, but protected mode OSes will stop the program ('segfault').
    If you WRITE to it, then either the OS catches the attempt, or you have
    good chances of locking up the machine.

    > Some example code, if
    > possible, would also be appreciated.
    >
    > Thanks in advance.
    >
    >
    > -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
    > http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
    > -----== Over 80,000 Newsgroups - 16 Different Servers! =-----
    >
    Mr. 4X, Jul 10, 2003
    #6
  7. Denis Palmeiro

    Micah Cowan Guest

    (prashna) writes:

    > If the pointer is not initialised expicitly, does'nt it get
    > initialised to NULL by default.


    NO. The only time this will happen without an explicit initializer is
    when it is statically allocated.

    > I have written the following program
    > which shows that by default any pointer will be intialised to
    > NULL.Have a look at the program.
    >
    > #include <stdio.h>
    >
    > int main()
    > {
    > int *p;
    > if (p ==NULL)
    > printf("pointer is null %p\n",p);
    > else
    > printf("pointer is not null\n");
    >
    > }
    >
    > and here is the output,
    > $ cc dereference_null.c
    > $ ./a.out
    > pointer is null 0
    >
    > or does it depends on the system that is being used?I executed this
    > program on AIX.


    You were simply unlucky.

    -Micah
    Micah Cowan, Jul 11, 2003
    #7
  8. Denis Palmeiro

    Guest

    Hi,

    I know that NULL is defined as 0. But there are chances that compiler
    may generate warnings (invalid pointer assignments) when you do this
    --

    int * iPtr = NULL; /* maybe illegal */

    So to be on SAFE side, I had typecasted it to int *, so to avoid
    any compiler warnings...if it is INTELLIGENT.

    So you are just complementing my answer abt de referencing NULL
    pointer :):)

    > Please do not top post. I actually thought that Jun's post was a
    > correct answer. Your post has some odd constructions. For example,
    > you do not need to cast NULL explicitly.
    >
    > NULL is a macro defined in stddef.h. to be the null pointer constant.
    > A null pointer constant is an integer constant expression with the
    > value 0 or such an expression casted to void*. A null pointer
    > constant converted to a pointer type is called a null pointer.
    >
    > #include <stddef.h>
    >
    > int* ip = NULL;
    > *ip; // undefined behavior
    >
    > would be sufficient. The problem with derefencing a null pointer is
    > that it invokes undefined behavior. It is impossible to know how any
    > particular implementation will handle it.
    >
    > Best wishes,
    >
    > Bob
    , Jul 14, 2003
    #8
  9. Denis Palmeiro

    Ben Pfaff Guest

    writes:

    > I know that NULL is defined as 0.


    It may also be defined as ((void *) 0) or other variations as
    well.

    > But there are chances that compiler may generate warnings
    > (invalid pointer assignments) when you do this --
    >
    > int * iPtr = NULL; /* maybe illegal */


    That's not ever illegal (and not invalid unless you failed to
    define NULL by including an appropriate header). It is perfectly
    fine as specified by the language definition.

    > So to be on SAFE side, I had typecasted it to int *, so to avoid
    > any compiler warnings...if it is INTELLIGENT.


    I'd be very surprised to have a compiler complain about the
    above, but compilers are allowed to warn about anything.
    --
    "To get the best out of this book, I strongly recommend that you read it."
    --Richard Heathfield
    Ben Pfaff, Jul 14, 2003
    #9
  10. On 14 Jul 2003 10:47:49 -0700, in comp.lang.c ,
    wrote:

    >Hi,

    please don't top post. Your response should go BELOW the text it
    relates to.

    >I know that NULL is defined as 0. But there are chances that compiler
    >may generate warnings (invalid pointer assignments) when you do this
    >--


    don't put two dashes and a space in the middle of your post. That
    separates a sig from the body, and causes all good newsreaders to snip
    all the text below. I had to force Agent to include it.

    >int * iPtr = NULL; /* maybe illegal */


    thats never illegal in declaration.

    >So to be on SAFE side, I had typecasted it to int *,


    Thats unnecessary. In C. Are you accidentally compilng in C++ mode?

    --
    Mark McIntyre
    CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
    CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>


    ----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
    ---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
    Mark McIntyre, Jul 14, 2003
    #10
  11. Denis Palmeiro

    Shill Guest

    >>may generate warnings (invalid pointer assignments) when you do this
    >>--

    >
    > don't put two dashes and a space in the middle of your post. That
    > separates a sig from the body, and causes all good newsreaders to snip
    > all the text below. I had to force Agent to include it.


    Hmmm, there was no space after the two hyphens :)
    Shill, Jul 16, 2003
    #11
    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:
    6
    Views:
    433
    Alf P. Steinbach
    Oct 3, 2007
  2. Tony Jackson

    Avoiding dereference of NULL pointer

    Tony Jackson, Dec 15, 2007, in forum: C Programming
    Replies:
    17
    Views:
    765
    Randy Howard
    Dec 29, 2007
  3. aneuryzma
    Replies:
    3
    Views:
    693
    Jim Langston
    Jun 16, 2008
  4. Armen Tsirunyan
    Replies:
    18
    Views:
    511
    James Kanze
    Oct 5, 2010
  5. Christopher
    Replies:
    4
    Views:
    431
    Ruben Safir
    Jul 9, 2011
Loading...

Share This Page