[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

A C without void*

Spiros Bousbouras

3/20/2011 8:01:00 PM

Why does C have void* ? Imagine a C' where the functionality that void*
provides would be provided instead by unsigned char* plus what
unsigned char* usually does. In what way would C' be inferior to C ?
78 Answers

Nick

3/20/2011 8:08:00 PM

0

Spiros Bousbouras <spibou@gmail.com> writes:

> Why does C have void* ? Imagine a C' where the functionality that void*
> provides would be provided instead by unsigned char* plus what
> unsigned char* usually does. In what way would C' be inferior to C ?

Imagine? Some of us remember it! (modulo the "unsigned" bit).
--
Online waterways route planner | http://ca...
Plan trips, see photos, check facilities | http://canalp...

Lauri Alanko

3/20/2011 8:14:00 PM

0

In article <%1thp.70888$2_5.22463@newsfe25.ams2>,
Spiros Bousbouras <spibou@gmail.com> wrote:
> Why does C have void* ? Imagine a C' where the functionality that void*
> provides would be provided instead by unsigned char* plus what
> unsigned char* usually does. In what way would C' be inferior to C ?

A void pointer can be converted without an explicit cast (though this
is as much a pitfall as a convenience). Also, a void pointer cannot be
dereferenced or used in pointer arithmetic, so some errors are
detected that would go unnoticed if char pointers were used.


Lauri

Ben Pfaff

3/20/2011 8:18:00 PM

0

Spiros Bousbouras <spibou@gmail.com> writes:

> Why does C have void* ? Imagine a C' where the functionality that void*
> provides would be provided instead by unsigned char* plus what
> unsigned char* usually does. In what way would C' be inferior to C ?

C' would require more casts.
--
Ben Pfaff
http://be...

Ian Collins

3/20/2011 8:18:00 PM

0

On 03/21/11 09:00 AM, Spiros Bousbouras wrote:
> Why does C have void* ? Imagine a C' where the functionality that void*
> provides would be provided instead by unsigned char* plus what
> unsigned char* usually does. In what way would C' be inferior to C ?

I guess you haven't been programming in C long enough to remember K&R C?

--
Ian Collins

Keith Thompson

3/20/2011 8:51:00 PM

0

Spiros Bousbouras <spibou@gmail.com> writes:
> Why does C have void* ? Imagine a C' where the functionality that void*
> provides would be provided instead by unsigned char* plus what
> unsigned char* usually does. In what way would C' be inferior to C ?

It would fail to distinguish between a pointer that points to data
whose type is not explicitly stated, and a pointer that points to
character data. (See pre-ANSI K&R C.)

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.ne...
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

J. J. Farrell

3/21/2011 12:30:00 AM

0

Spiros Bousbouras wrote:
> Why does C have void* ? Imagine a C' where the functionality that void*
> provides would be provided instead by unsigned char* plus what
> unsigned char* usually does. In what way would C' be inferior to C ?

Why imagine it when we can remember it? Many of us used it for years
(give or take 'unsigned'), it's possible that some still do - some
certainly have to work with code which suffers from the legacy. We
remember what an improvement the addition of void was, both for the
documentary effect of explicitly having a pointer to 'something
unspecified' as distinct from a pointer to char, and also for how it got
rid of so many ugly casts.

John Doe

3/21/2011 2:00:00 AM

0

On Sun, 20 Mar 2011 20:00:59 +0000, Spiros Bousbouras wrote:

> Why does C have void* ? Imagine a C' where the functionality that void*
> provides would be provided instead by unsigned char* plus what
> unsigned char* usually does. In what way would C' be inferior to C ?

Having void* allows the programmer to specify that the type of data to
which the pointer points is unspecified, rather than having to lie to keep
the compiler happy.

William Ahern

3/21/2011 2:59:00 AM

0

Spiros Bousbouras <spibou@gmail.com> wrote:
> Why does C have void* ? Imagine a C' where the functionality that void*
> provides would be provided instead by unsigned char* plus what
> unsigned char* usually does. In what way would C' be inferior to C ?

In all the same ways that C++ is inferior to C. If his book is to be
believed, Stroustrup designed C++ to stop people from casting so much. The
type-intelligent OOP semantics were the carrot, and the ugly casting
notations--static_cast<>(), et al--were the stick.

But if you read actual C++ code in the wild, it's thick with explicit casts.
In fact, so much so that one would think the design must somehow
fundamentally encourage it.

On the other hand, I rarely see casting in C. Void pointers don't seem to be
abused or used much at all, in my experience. If used at all it's not to
acquire a different type for the reference, but merely for passing generic
objects.

Void pointers are the simple workaround to the caveats of strict typing.
C++'s typing semantics present the complex "solution". Both are an attempt
to provide a safer--compiler instrumented--workaround to strict typing than
explicit casting.

Ian Collins

3/21/2011 3:52:00 AM

0

On 03/21/11 03:58 PM, William Ahern wrote:
>
> But if you read actual C++ code in the wild, it's thick with explicit casts.
> In fact, so much so that one would think the design must somehow
> fundamentally encourage it.

Not in mine you wont. I hardly see any in other people's code I work
with either.

--
Ian Collins

William Ahern

3/21/2011 4:33:00 AM

0

Ian Collins <ian-news@hotmail.com> wrote:
> On 03/21/11 03:58 PM, William Ahern wrote:
> >
> > But if you read actual C++ code in the wild, it's thick with explicit
> > casts. In fact, so much so that one would think the design must somehow
> > fundamentally encourage it.

> Not in mine you wont. I hardly see any in other people's code I work
> with either.

I've been making that braven assertion for years, including to my C++
enthusiast friends, and they usually avert their eyes with guilt. (Though
whether they should feel guilty is another matter.) But perhaps I should put
on my TODO list an explicit conversion analyzer. This shouldn't be too
difficult with clang. It would be ironic if this proved me wrong, because
clang is itself rife with casts.

william:% pwd
/usr/local/llvm/src/llvm-trunk/tools/clang
william:% grep -r cast . | grep '>(' | grep -v .svn | wc -l
6021

But I don't do much C++ programming, and maybe that one-liner commits some
gross errors.