Topic: Why do we have void*?


Author: "Greg Herlihy" <greghe@pacbell.net>
Date: Wed, 17 May 2006 13:51:56 CST
Raw View
David R Tribble wrote:
> Tom   s wrote:
> >> This is probably more of a question for a C newsgroup, but nonetheless C++
> >> is now a language in and of itself -- so everything goes.
> >>
> >> Every object is made up of bytes, and a "char*" can store the address of a
> >> byte. So why do we have "void*" in the language?
> >>
> >> >From what I can see, "char*" is perfectly sufficient for storing any
> >> address. Plus, it has the added functionality of pointer arithmetic.
> >
>
> Kuyper wrote:
> > The purpose of void* is type safety. It's the things you can't do with
> > void* that you can do with char* which make void* so valuable. You
> > can't dereference it, or even do address arithmetic on it, without
> > first converting to some other type, and it won't implicity convert to
> > other types. When code uses void*, it's explicitly saying "I don't know
> > what type of object this pointer is pointing at", which is a very
> > important thing to keep track of, when it's true.  This is particularly
> > important in C++, where you have function overloading; you should be
> > able to distinguish the overload which is intended to handle pointers
> > to unknown types from the overload for a pointer to char, and you
> > couldn't do that if char* was used for both purposes.
>
> Yep.  Consider 'void' equivalent to 'untyped' or 'notype', i.e.,
> 'void*' is 'pointer to untyped memory byte(s)'.  It would probably
> have been less confusing if they'd chosen a different word than
> 'void', but that's water under the bridge now.
>
..
> And it's useful for providing an "opaque" pointer to an object
> of some type that you want to keep hidden from client code.
> Your code allocates the object and passes its pointer back
> as an untyped pointer to the client, and the client passes it
> back to your code without knowing what it really points to,
> and you convert it to the proper pointer type within your code.

An opaque pointer should be declared as a pointer to a
forwardly-declared, but never defined type - not as a pointer to void.
Otherwise the client will be free to pass any kind of pointer to a
routine that requires a pointer to the opaque type. Only in the case
that a function is actually willing to accept a pointer to any type,
should it declare a void * parameter.

Greg


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: stephan.bergmann@sun.com (Stephan Bergmann)
Date: Thu, 18 May 2006 05:53:57 GMT
Raw View
David R Tribble wrote:
[...]
> And it's useful for providing an "opaque" pointer to an object
> of some type that you want to keep hidden from client code.
> Your code allocates the object and passes its pointer back
> as an untyped pointer to the client, and the client passes it
> back to your code without knowing what it really points to,
> and you convert it to the proper pointer type within your code.
[...]
> -drt

In such a case, it is probably even better (i.e., more type safe) to
leave the object type incomplete for the client code, and use a pointer
to that incomplete object type, instead of a void pointer, at the interface.

-Stephan

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: "David R Tribble" <david@tribble.com>
Date: Thu, 18 May 2006 18:41:54 CST
Raw View
David R Tribble wrote:
>> And [void*] is useful for providing an "opaque" pointer to an object
>> of some type that you want to keep hidden from client code.
>> Your code allocates the object and passes its pointer back
>> as an untyped pointer to the client, and the client passes it
>> back to your code without knowing what it really points to,
>> and you convert it to the proper pointer type within your code.
>

Greg Herlihy wrote:
> An opaque pointer should be declared as a pointer to a
> forwardly-declared, but never defined type - not as a pointer to void.
> Otherwise the client will be free to pass any kind of pointer to a
> routine that requires a pointer to the opaque type. Only in the case
> that a function is actually willing to accept a pointer to any type,
> should it declare a void * parameter.

Yep, such as when your code has to store an opaque pointer to
an object managed by some other third-party code.  That third-party
code might vary quite a lot (if you link to different libraries on
different platforms, for instance), so you're forced to store the
pointer in its most generic form, which is a void*.

-drt

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: NULL@NULL.NULL ("Tom s")
Date: Mon, 15 May 2006 21:13:09 GMT
Raw View
This is probably more of a question for a C newsgroup, but nonetheless C+=
+=20
is now a language in and of itself -- so everything goes.

Every object is made up of bytes, and a "char*" can store the address of =
a=20
byte. So why do we have "void*" in the language?

>From what I can see, "char*" is perfectly sufficient for storing any=20
address. Plus, it has the added functionality of pointer arithmetic.


-Tom=E1s

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: "Gene Bushuyev" <spam@spamguard.com>
Date: Mon, 15 May 2006 23:36:52 CST
Raw View
""Tom   s"" <NULL@NULL.NULL> wrote in message
news:ue4ag.9209$j7.305609@news.indigo.ie...

This is probably more of a question for a C newsgroup, but nonetheless C++
is now a language in and of itself -- so everything goes.

Every object is made up of bytes, and a "char*" can store the address of a
byte. So why do we have "void*" in the language?

>From what I can see, "char*" is perfectly sufficient for storing any
address. Plus, it has the added functionality of pointer arithmetic.


void* is the only pointer type that you can cast the pointer to any other object
type to and from and get exactly what you had before.

--
Gene Bushuyev (www.gbresearch.com)
----------------------------------------------------------------
There is no greatness where there is no simplicity, goodness and truth. ~ Leo
Tolstoy

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: NULL@NULL.NULL ("Tom s")
Date: Tue, 16 May 2006 14:53:51 GMT
Raw View
Gene Bushuyev posted:


> void* is the only pointer type that you can cast the pointer to any
> other object type to and from and get exactly what you had before.

A "char*" should have that exact ability, as it can address a single byte=
=20
-- the smallest chunk of memory in C++.

My argument is that a char* has all the functionality of a void*, so what=
=20
can a void* do that a char* can't? char* even has the added bonus of=20
pointer arithmetic.

-Tom=E1s

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: "Kapil" <navkapil@gmail.com>
Date: Tue, 16 May 2006 09:56:07 CST
Raw View
I think this is really to stop you from doing pointer airthmetic if you
dont know what type of object is. Isn't it sound in regard of Software
Engineering Principles

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: kuyper@wizard.net
Date: Tue, 16 May 2006 10:45:42 CST
Raw View
"Tom   s" wrote:
> This is probably more of a question for a C newsgroup, but nonetheless C++
> is now a language in and of itself -- so everything goes.
>
> Every object is made up of bytes, and a "char*" can store the address of a
> byte. So why do we have "void*" in the language?
>
> >From what I can see, "char*" is perfectly sufficient for storing any
> address. Plus, it has the added functionality of pointer arithmetic.

The purpose of void* is type safety. It's the things you can't do with
void* that you can do with char* which make void* so valuable. You
can't dereference it, or even do address arithmetic on it, without
first converting to some other type, and it won't implicity convert to
other types. When code uses void*, it's explicitly saying "I don't know
what type of object this pointer is pointing at", which is a very
important thing to keep track of, when it's true.  This is particularly
important in C++, where you have function overloading; you should be
able to distinguish the overload which is intended to handle pointers
to unknown types from the overload for a pointer to char, and you
couldn't do that if char* was used for both purposes.


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: NULL@NULL.NULL ("Tom s")
Date: Wed, 17 May 2006 06:07:00 GMT
Raw View
Kuyper posted:

> The purpose of void* is type safety. It's the things you can't do with
> void* that you can do with char* which make void* so valuable. You
> can't dereference it, or even do address arithmetic on it, without
> first converting to some other type, and it won't implicity convert to
> other types. When code uses void*, it's explicitly saying "I don't know
> what type of object this pointer is pointing at", which is a very
> important thing to keep track of, when it's true.  This is particularly
> important in C++, where you have function overloading; you should be
> able to distinguish the overload which is intended to handle pointers
> to unknown types from the overload for a pointer to char, and you
> couldn't do that if char* was used for both purposes.


I see, nice explanation, thanks.

Am I right though in thinking that a "char*" can store any address reliab=
ly?=20
(I'm pretty sure of this, but just when you're sure about something, some=
one=20
points something out which you missed the first time round).


-Tom=E1s

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: yozara@terra.es (Zara)
Date: Wed, 17 May 2006 06:07:48 GMT
Raw View
On Tue, 16 May 2006 14:53:51 GMT, NULL@NULL.NULL ("Tom=E1s") wrote:

>Gene Bushuyev posted:
>
>
>> void* is the only pointer type that you can cast the pointer to any
>> other object type to and from and get exactly what you had before.
>
>A "char*" should have that exact ability, as it can address a single byt=
e=20
>-- the smallest chunk of memory in C++.
>
>My argument is that a char* has all the functionality of a void*, so wha=
t=20
>can a void* do that a char* can't? char* even has the added bonus of=20
>pointer arithmetic.
>
>-Tom=E1s

The greatest adavantage of "void *" over "char *" is that it can never
be (mis)used by itself, you need to reinterpret_cast it.

char * may be inadvertently used within a stream operation. If you
were trying to see the numerical value of the pointer, you might get
lots of grabage and (probably) some kind of memory access error.


Zara

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: "David R Tribble" <david@tribble.com>
Date: Wed, 17 May 2006 01:07:27 CST
Raw View
Tom   s wrote:
>> This is probably more of a question for a C newsgroup, but nonetheless C++
>> is now a language in and of itself -- so everything goes.
>>
>> Every object is made up of bytes, and a "char*" can store the address of a
>> byte. So why do we have "void*" in the language?
>>
>> >From what I can see, "char*" is perfectly sufficient for storing any
>> address. Plus, it has the added functionality of pointer arithmetic.
>

Kuyper wrote:
> The purpose of void* is type safety. It's the things you can't do with
> void* that you can do with char* which make void* so valuable. You
> can't dereference it, or even do address arithmetic on it, without
> first converting to some other type, and it won't implicity convert to
> other types. When code uses void*, it's explicitly saying "I don't know
> what type of object this pointer is pointing at", which is a very
> important thing to keep track of, when it's true.  This is particularly
> important in C++, where you have function overloading; you should be
> able to distinguish the overload which is intended to handle pointers
> to unknown types from the overload for a pointer to char, and you
> couldn't do that if char* was used for both purposes.

Yep.  Consider 'void' equivalent to 'untyped' or 'notype', i.e.,
'void*' is 'pointer to untyped memory byte(s)'.  It would probably
have been less confusing if they'd chosen a different word than
'void', but that's water under the bridge now.

It's primarily used for passing pointers to objects when you don't
care what the actual type of the objects are, when you want to
manipulate the object as just collections of bytes (which is
what the malloc() and free() functions do).

It's also useful when you want to stash a pointer to an object
provided by some code that is outside your own code, so that
you can return it to that outside code later on; you don't care
what the object actually is, you just want to save its address
for later use.

And it's useful for providing an "opaque" pointer to an object
of some type that you want to keep hidden from client code.
Your code allocates the object and passes its pointer back
as an untyped pointer to the client, and the client passes it
back to your code without knowing what it really points to,
and you convert it to the proper pointer type within your code.

All of these things could be done using 'char*', but then you
don't get the benefit of type safety, because the compiler
assumes (rightly) that it's a pointer to a character object,
when in fact it probably is not.

-drt


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: "kanze" <kanze@gabi-soft.fr>
Date: Wed, 17 May 2006 11:35:45 CST
Raw View
"Tom   s" wrote:

> Am I right though in thinking that a "char*" can store any
> address reliably?

Yes.  Void was introduced relatively late in C, for reasons of
type safety.  Before that, char* was what was used, and it was
felt necessary to support existing code.

The other point, of course, is that unsigned char (and in C++,
char as well) has been blessed as a means of accessing raw
memory.  Since you look at any object as an array of char, char*
pretty much has to be able to address any object.

--
James Kanze                                           GABI Software
Conseils en informatique orient   e objet/
                   Beratung in objektorientierter Datenverarbeitung
9 place S   mard, 78210 St.-Cyr-l'   cole, France, +33 (0)1 30 23 00 34


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]