Re: pointer doubt (1)

Discussion in 'C Programming' started by Joe Pfeiffer, May 8, 2013.

  1. Joe Pfeiffer

    Joe Pfeiffer Guest

    rashan <> writes:

    > Can ne1 explain me the difference between.... a NULL pointer ... and a
    > VOID pointer.
    >
    > With Many Thanks.


    One is a particular pointer, and the other is a pointer type.

    A null pointer is a pointer with a special value that be pointing to
    a valid memory object; you can set a pointer to NULL to indicate that
    it's invalid and you can test it against NULL to see if it not valid
    (note the careful wording; unfortunately, you can have a pointer
    containing an essentially random value that isn't pointing to a valid
    object as well. So if it's pointing to NULL you know it's invalid; if
    it's pointing elsewhere it's only pointing to something valid if the
    programmer made it do so).

    By a void pointer, I assume you mean something like

    void *ptr;

    This is a pointer to "memory" of no particular type, which can be cast
    to any pointer type you want. It can point to NULL or to somewhere
    else.

    So if you have

    #include <malloc.h>

    int *bogus;

    bogus = malloc(sizeof int);

    then
    (1) bogus is a pointer to int
    (2) malloc() returns a void*
    (3) My call to malloc() requests a pointer to a blob of memory the
    right size to hold an int. On a 32 bit system, this is probably
    4 bytes.
    (4) If the call to malloc() returns NULL, it means it wasn't able to
    allocate the requested memory.
    (5) The type of the pointer is cast to int* as part of the
    assignment.
    (6) you can now test

    if (bogus == NULL) { ...something... }

    to see if malloc() succeeded, and if it did you can say things like

    *bogus = 3;

    (so the int-size piece of memory pointed to by bogus will now
    contain the value 3)

    Also see:

    http://c-faq.com/ptrs/
    http://c-faq.com/~scs/cgi-bin/faqcat.cgi?sec=null
     
    Joe Pfeiffer, May 8, 2013
    #1
    1. Advertising

  2. Joe Pfeiffer

    Ike Naar Guest

    On 2013-05-08, Joe Pfeiffer <> wrote:
    > So if you have
    >
    > #include <malloc.h>


    Nonstandard header.
    The standard header for malloc() is <stdlib.h>

    > int *bogus;
    >
    > bogus = malloc(sizeof int);


    That won't compile; it should be either

    bogus = malloc(sizeof *bogus);

    or

    bogus = malloc(sizeof (int)); /* note the parentheses */
     
    Ike Naar, May 8, 2013
    #2
    1. Advertising

  3. Joe Pfeiffer <> writes:
    > rashan <> writes:
    >
    >> Can ne1 explain me the difference between.... a NULL pointer ... and a
    >> VOID pointer.
    >>
    >> With Many Thanks.

    >
    > One is a particular pointer, and the other is a pointer type.
    >
    > A null pointer is a pointer with a special value that be pointing to
    > a valid memory object; you can set a pointer to NULL to indicate that
    > it's invalid and you can test it against NULL to see if it not valid
    > (note the careful wording; unfortunately, you can have a pointer
    > containing an essentially random value that isn't pointing to a valid
    > object as well. So if it's pointing to NULL you know it's invalid; if
    > it's pointing elsewhere it's only pointing to something valid if the
    > programmer made it do so).


    I suggest that the phrase "pointing to NULL", or even "pointing to
    null", is misleading.

    A null pointer doesn't point to null, it *is* null. More precisely, a
    pointer object whose value has been set to NULL contains a null pointer
    value; that pointer value doesn't point to *anything*.

    A pointer value can be any of the following:

    1. A valid pointer to some object;
    2. A pointer just past the end of an array;
    3. A null pointer, which doesn't point to anything; or
    4. An invalid/garbage pointer.

    Of these, only (1) can be dereferenced, only (1) and (2) can be
    compared with "<", "<=", ">", or ">=", and only (1), (2), and (3)
    can be compared with "==" or "!=" or even examined. Violating any
    of these rules results in undefined behavior (i.e., don't count on
    getting an error message).

    Invalid/garbage pointers can be the result of an uninitialized
    pointer object, a pointer to an object that no longer exists,
    or doing something that has undefined behavior.

    [...]

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Working, but not speaking, for JetHead Development, Inc.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, May 8, 2013
    #3
  4. Keith Thompson <> wrote:

    (snip)

    > I suggest that the phrase "pointing to NULL", or even "pointing to
    > null", is misleading.


    > A null pointer doesn't point to null, it *is* null. More precisely, a
    > pointer object whose value has been set to NULL contains a null pointer
    > value; that pointer value doesn't point to *anything*.


    Hmm. If "null" is a synonm for "nothing,", and an antonym for
    "anything" then "point to null" seems to mean the same thing as
    "point to nothing" or "not point to anything."

    I suppose one could say "has a value that compares equal to the
    NULL pointer constant", but "points to null" is a lot easier to
    say and write.

    -- glen
     
    glen herrmannsfeldt, May 9, 2013
    #4
  5. Joe Pfeiffer

    Joe Pfeiffer Guest

    Ike Naar <> writes:

    > On 2013-05-08, Joe Pfeiffer <> wrote:
    >> So if you have
    >>
    >> #include <malloc.h>

    >
    > Nonstandard header.
    > The standard header for malloc() is <stdlib.h>
    >
    >> int *bogus;
    >>
    >> bogus = malloc(sizeof int);

    >
    > That won't compile; it should be either
    >
    > bogus = malloc(sizeof *bogus);
    >
    > or
    >
    > bogus = malloc(sizeof (int)); /* note the parentheses */


    Thank you. That's what I get for not compiling my examples.
     
    Joe Pfeiffer, May 9, 2013
    #5
  6. Joe Pfeiffer

    James Kuyper Guest

    On 05/08/2013 08:33 PM, glen herrmannsfeldt wrote:
    > Keith Thompson <> wrote:
    >
    > (snip)
    >
    >> I suggest that the phrase "pointing to NULL", or even "pointing to
    >> null", is misleading.

    >
    >> A null pointer doesn't point to null, it *is* null. More precisely, a
    >> pointer object whose value has been set to NULL contains a null pointer
    >> value; that pointer value doesn't point to *anything*.

    >
    > Hmm. If "null" is a synonm for "nothing,", and an antonym for
    > "anything" then "point to null" seems to mean the same thing as
    > "point to nothing" or "not point to anything."


    Historically, the use of "null" to describe pointers was not intended to
    describe what the pointer points at, but rather how it's represented. On
    many systems, particularly at that time, and particularly the ones C was
    first developed on, there was no practical difference between pointers
    an integers of the same size, and a null pointer was a pointer that had
    the same representation as an integer 0. This is not mandated by the C
    standard, and there's many systems where it's not true, but that it the
    origin of the terminology.

    > I suppose one could say "has a value that compares equal to the
    > NULL pointer constant", but "points to null" is a lot easier to
    > say and write.


    "null pointer" is even easier, and has the advantage of being a
    standard-defined term.
    --
    James Kuyper
     
    James Kuyper, May 9, 2013
    #6
  7. Joe Pfeiffer

    James Kuyper Guest

    On 05/08/2013 08:33 PM, glen herrmannsfeldt wrote:
    > Keith Thompson <> wrote:
    >
    > (snip)
    >
    >> I suggest that the phrase "pointing to NULL", or even "pointing to
    >> null", is misleading.

    >
    >> A null pointer doesn't point to null, it *is* null. More precisely, a
    >> pointer object whose value has been set to NULL contains a null pointer
    >> value; that pointer value doesn't point to *anything*.

    >
    > Hmm. If "null" is a synonm for "nothing,", and an antonym for
    > "anything" then "point to null" seems to mean the same thing as
    > "point to nothing" or "not point to anything."


    Historically, the use of "null" to describe pointers was not intended to
    describe what the pointer points at, but rather how it's represented. On
    many systems, particularly at that time, and particularly the ones C was
    first developed on, there was no practical difference between pointers
    an integers of the same size, and a null pointer was a pointer that had
    the same representation as an integer 0. This is not mandated by the C
    standard, and there's many systems where it's not true, but that it the
    origin of the terminology.

    > I suppose one could say "has a value that compares equal to the
    > NULL pointer constant", but "points to null" is a lot easier to
    > say and write.


    "null pointer" is even easier, and has the advantage of being a
    standard-defined term.
    --
    James Kuyper
     
    James Kuyper, May 9, 2013
    #7
    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:
    10
    Views:
    745
    Chris Torek
    Feb 4, 2005
  2. Bob Nelson

    doubt about doubt

    Bob Nelson, Jul 28, 2006, in forum: C Programming
    Replies:
    11
    Views:
    661
  3. Replies:
    0
    Views:
    593
  4. Peter Otten
    Replies:
    2
    Views:
    140
    Cousin Stanley
    Aug 10, 2013
  5. Terry Reedy
    Replies:
    0
    Views:
    137
    Terry Reedy
    Aug 10, 2013
Loading...

Share This Page