Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Re: pointer doubt (1)

Reply
Thread Tools

Re: pointer doubt (1)

 
 
Joe Pfeiffer
Guest
Posts: n/a
 
      05-08-2013
rashan <(E-Mail Removed)> 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



 
Reply With Quote
 
 
 
 
Ike Naar
Guest
Posts: n/a
 
      05-08-2013
On 2013-05-08, Joe Pfeiffer <(E-Mail Removed)> 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 */
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      05-08-2013
Joe Pfeiffer <(E-Mail Removed)> writes:
> rashan <(E-Mail Removed)> 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.velocityreviews.com/forums/(E-Mail Removed) <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"
 
Reply With Quote
 
glen herrmannsfeldt
Guest
Posts: n/a
 
      05-09-2013
Keith Thompson <(E-Mail Removed)> 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
 
Reply With Quote
 
Joe Pfeiffer
Guest
Posts: n/a
 
      05-09-2013
Ike Naar <(E-Mail Removed)> writes:

> On 2013-05-08, Joe Pfeiffer <(E-Mail Removed)> 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.
 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      05-09-2013
On 05/08/2013 08:33 PM, glen herrmannsfeldt wrote:
> Keith Thompson <(E-Mail Removed)> 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
 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      05-09-2013
On 05/08/2013 08:33 PM, glen herrmannsfeldt wrote:
> Keith Thompson <(E-Mail Removed)> 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
 
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
dotnet doubt can any body clarify my doubt challa462@gmail.com ASP .Net 0 08-22-2012 06:02 AM
Pointer to pointer Vs References to Pointer bansalvikrant@gmail.com C++ 4 07-02-2009 10:20 AM
doubt about doubt Bob Nelson C Programming 11 07-30-2006 08:17 PM
passing the address of a pointer to a func that doesnt recieve a pointer-to-a-pointer jimjim C Programming 16 03-27-2006 11:03 PM
Pointer-to-pointer-to-pointer question masood.iqbal@lycos.com C Programming 10 02-04-2005 02:57 AM



Advertisments