Emulov's Stupid Questions: nr 22 
Author Message
 Emulov's Stupid Questions: nr 22

I just read up on a thing called a void pointer. The document stated that a
void pointer can be used if the type the pointer points to is unknown. My
question is: Why not always use a void pointer if it's true? So then you
never have to worry that the types match... I'm sure I'll get a lot of
respones, some good, some bad... :-)

--
Hey, I speak for myself. No _nagging_ afterwards...
remove NOSPAM when replying
-
http://www.*-*-*.com/ ~emulov/index_english.htm
computer, internet, programming, search engines



Tue, 02 Jan 2001 03:00:00 GMT  
 Emulov's Stupid Questions: nr 22

Quote:

> I just read up on a thing called a void pointer. The document stated that a
> void pointer can be used if the type the pointer points to is unknown. My
> question is: Why not always use a void pointer if it's true? So then you
> never have to worry that the types match...

You can't dereference a (void *), because that involves using an incomplete
type, as the (void) type cannot be completed.  For the same reason, you
can't do pointer arithmetic on a (void *).

A generic pointer is intended to be used as a pointer to any object, but it
must always be cast back to a pointer to the original type (or a pointer to
unsigned char) before the referenced object is used.  It is useful for
(among other things) writing generic containers in C: the code using the
container passes a pointer to some object as a (void *), which the container
can store.  The calling code can later retrieve the (void *), cast it back
to a pointer to an object type, and then indirect through that pointer.

Note that conversion between (void *) and any object pointer type is always
legal in C, and does not require an explicit cast.

--



Tue, 02 Jan 2001 03:00:00 GMT  
 Emulov's Stupid Questions: nr 22
: I just read up on a thing called a void pointer. The document stated that a
: void pointer can be used if the type the pointer points to is unknown. My
: question is: Why not always use a void pointer if it's true? So then you
: never have to worry that the types match... I'm sure I'll get a lot of
: respones, some good, some bad... :-)

Because you can't dereference a void pointer.



Tue, 02 Jan 2001 03:00:00 GMT  
 Emulov's Stupid Questions: nr 22

Quote:

> I just read up on a thing called a void pointer. The document stated that a
> void pointer can be used if the type the pointer points to is unknown. My
> question is: Why not always use a void pointer if it's true? So then you
> never have to worry that the types match... I'm sure I'll get a lot of
> respones, some good, some bad... :-)

> --
> Hey, I speak for myself. No _nagging_ afterwards...
> remove NOSPAM when replying
> -
> http://www.sin.khk.be/~emulov/index_english.htm
> computer, internet, programming, search engines

I would think error checking at compile time would be difficult using (void *).


Tue, 02 Jan 2001 03:00:00 GMT  
 Emulov's Stupid Questions: nr 22

Quote:

>I just read up on a thing called a void pointer. The document stated that a
>void pointer can be used if the type the pointer points to is unknown.

If that's exactly what it said, it's a little misleading.  You can't
"use" a void pointer like an ordinary object pointer: specifically, you
can't dereference it or do pointer arithmetic on it.  (After all, you
don't know what it points to, right?  So you don't know the type of the
result for dereferencing, and you don't know the size of the pointed-to
object for arithmetic.)

What you can do is to temporarily stash any object pointer (though not,
technically, a function pointer) in a void *, and then get it back out
again later.  It's your responsibility, when you retrieve the value,
to make sure you convert it back to the right type (usually by assigning
it to a pointer of the correct type; casting works too).  For example,
it's a serious error to take an int *, stash it in a void *, and then
assign it back to a float *.  Unlike the case where you directly assign
an int * to a float *, this is unlikely to be spotted by the compiler.

So void * doesn't free you of worrying about pointer types.  If anything,
it makes them more of a concern, since using void * takes away some of
the protection you normally get from the implementation.

Cheers,
Richard
--
Richard Stamp
Churchill College, Cambridge



Tue, 02 Jan 2001 03:00:00 GMT  
 Emulov's Stupid Questions: nr 22

Quote:

>I just read up on a thing called a void pointer. The document stated that a
>void pointer can be used if the type the pointer points to is unknown. My
>question is: Why not always use a void pointer if it's true? So then you
>never have to worry that the types match... I'm sure I'll get a lot of
>respones, some good, some bad... :-)

A void pointer is an incomplete type.  It is good for storing a pointer to
another data type.  I use them for information hiding in C sometimes, where
you provide a callback function and just expose a void pointer to some data
object.
Some things you can't do with a void pointer:
1.  Do pointer arithmetic on it.
From the C FAQ:
11.24:  Why can't I perform arithmetic on a void * pointer?

A:      The compiler doesn't know the size of the pointed-to objects.
        Before performing arithmetic, convert the pointer either to
        char * or to the pointer type you're trying to manipulate (but
        see also question 4.5).

        References: ANSI Sec. 3.1.2.5, Sec. 3.3.6; ISO Sec. 6.1.2.5,
        Sec. 6.3.6; H&S Sec. 7.6.2 p. 204.

2.  Get stuff out of it by dereferencing.

3.  Find out how big the stuff it points to is.

4.  I'm sure that there is lots of other stuff you can't do with them.
They are just handy little cans for storing other pointers in.

Try compiling this:
#include <stdio.h>
int main(int argc, char **argv)
{
    double     d = 5;
    double     e;
    size_t     i;
    void      *v;
    v = &d;
    e = *v;
    v++;
    i = sizeof(*v);
    return 0;

Quote:
}

--
Hypertext C-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-FAQ ftp: ftp://rtfm.mit.edu, C-FAQ Book: ISBN 0-201-84519-9
Try "C Programming: A Modern Approach" ISBN 0-393-96945-2
Want Software?  Algorithms?  Pubs? http://www.infoseek.com


Tue, 02 Jan 2001 03:00:00 GMT  
 Emulov's Stupid Questions: nr 22

Quote:

> A void pointer is an incomplete type.

On the contrary, a void pointer is a complete type; you can dereference a
pointer to one.  However, a void pointer *is* a pointer to an incomplete
type (and one which cannot be completed).

--



Tue, 02 Jan 2001 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Emulov's stupid questions: nr 23

2. Emulov's stupid questions: nr 24

3. Emulov's Stupid Questions: nr25 (I think, I lost count)

4. Emulov's 21st question

5. Emulov's question (nr 20?): an illegal while loop?

6. I'm caught in a catch 22.CRecordset problem

7. Question For VC4.22

8. Stupid Beginners' Question

9. Really stupid question about static linking to MFC dll's

10. Want nr sources (from Dr. Dobb's Journal)

11. Want nr sources (from Dr. Dobb's Journal)

12. Random numbers between 0 and 22.

 

 
Powered by phpBB® Forum Software