C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Why do we have void*?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards
View previous topic :: View next topic  
Author Message
Tomás
Guest





PostPosted: Mon May 15, 2006 9:21 pm    Post subject: Why do we have void*? Reply with quote



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?

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



-Tomás

---
[ 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 ]
Back to top
Gene Bushuyev
Guest





PostPosted: Tue May 16, 2006 5:21 am    Post subject: Re: Why do we have void*? Reply with quote



""Tomás"" <NULL (AT) NULL (DOT) NULL> wrote in message
news:ue4ag.9209$j7.305609 (AT) news (DOT) 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?

Quote:
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 ]
Back to top
Tomás
Guest





PostPosted: Tue May 16, 2006 3:24 pm    Post subject: Re: Why do we have void*? Reply with quote



Gene Bushuyev posted:


Quote:
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
-- the smallest chunk of memory in C++.

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

-Tomás

---
[ 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 ]
Back to top
Kapil
Guest





PostPosted: Tue May 16, 2006 3:24 pm    Post subject: Re: Why do we have void*? Reply with quote

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 ]
Back to top
Guest






PostPosted: Tue May 16, 2006 4:21 pm    Post subject: Re: Why do we have void*? Reply with quote

"Tomás" wrote:
Quote:
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 ]
Back to top
Tomás
Guest





PostPosted: Wed May 17, 2006 6:21 am    Post subject: Re: Why do we have void*? Reply with quote

Kuyper posted:

Quote:
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 reliably?
(I'm pretty sure of this, but just when you're sure about something, someone
points something out which you missed the first time round).


-Tomás

---
[ 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 ]
Back to top
Zara
Guest





PostPosted: Wed May 17, 2006 6:21 am    Post subject: Re: Why do we have void*? Reply with quote

On Tue, 16 May 2006 14:53:51 GMT, NULL (AT) NULL (DOT) NULL ("Tomás") wrote:

Quote:
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
-- the smallest chunk of memory in C++.

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

-Tomás

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 ]
Back to top
David R Tribble
Guest





PostPosted: Wed May 17, 2006 6:21 am    Post subject: Re: Why do we have void*? Reply with quote

Tomás wrote:
Quote:
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:
Quote:
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 ]
Back to top
kanze
Guest





PostPosted: Wed May 17, 2006 5:21 pm    Post subject: Re: Why do we have void*? Reply with quote

"Tomás" wrote:

Quote:
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 ]
Back to top
Greg Herlihy
Guest





PostPosted: Wed May 17, 2006 7:21 pm    Post subject: Re: Why do we have void*? Reply with quote

David R Tribble wrote:
Quote:
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 ]
Back to top
Stephan Bergmann
Guest





PostPosted: Thu May 18, 2006 6:21 am    Post subject: Re: Why do we have void*? Reply with quote

David R Tribble wrote:
[...]
Quote:
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 ]
Back to top
David R Tribble
Guest





PostPosted: Fri May 19, 2006 12:21 am    Post subject: Re: Why do we have void*? Reply with quote

David R Tribble wrote:
Quote:
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:
Quote:
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 ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.