🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #933

Re: Conversions to/from void*, redux

#933
From: Chip Salzenberg
Date: Mon, 11 Mar 1991 18:37
78 lines
3970 bytes
According to dhoyt@vx.acs.umn.edu:
>The only reason that you can point to any object with a char*, and
>sizeof( char ) == 1, is because K&R didn't have void.

You mean, it didn't have |void*|.  True enough.  But as this point
addresses the reasons for the language rules, instead of the rules
themselves and their implications, it is irrelevant.

>In article <27D8437B.F13@tct.uucp>, chip@tct.uucp (Chip Salzenberg) writes:
>>Point 1: ANSI created |void*| explicitly for the purpose of holding
>>any data address whatsoever.  ...  ANSI C++ cannot change this most
>>basic characteristic of |void*| without breaking compatibility with
>>many ANSI C programs and large portions of the ANSI C library.
>
>C++ is not C.  C++ breaks many compatibilty rules for Fortran.  So what?

Please re-read the key phrase of the above-quoted paragraph:

   "... without breaking compatibility with many ANSI C programs
    and large portions of the ANSI C library."

You may not care about compatibility with ANSI C.  Bjarne obviously
cared, or he wouldn't have made C++ an almost-exact superset of C.  I
care too.  I am therefore glad that the ANSI C++ committee has already
decided that where the ARM does not specify a language feature, the
ANSI C standard shall hold sway.  (That's an inexact description, but
the gist is true.)

>Are you using char*'s where void*'s are needed, either in ANSI C or in
>C++?  I doubt it.

Implicitly, sure I am.  Calling "char *p = new char[n]" is the only
way in C++ to get a block of uninitialized bytes, such as for use in
the smart array class that is currently at work in real code.

>Also it is not quite true that char* can point to anything; they can
>not point to functions in ANSI C.

My article, which you quoted above (apparently without reading it),
said in part:

    "ANSI created |void*| explicitly for the purpose of holding
     any data address whatsoever."
         ^^^^

>char* == void* equivalence in ANSI C is only there because there is a large
>body of code that uses char* to point to anything.  Void*'s can also point
>to anything.  char* == a, void* == a, void* == char*.  Blech.

Maybe you don't like it, but that's the way it is according to ANSI.
And it's not going to change.  Use it or leave it.

>I think that char*'s should point to chars and void*'s should point to a
>generic object.

ANSI guarantees that "sizeof(char) == 1".  Therefore, the sizes of all
objects must of necessity be multiples of the size of a char.  This
fact leads to the unavoidable conclusion that a |char*| must be able
to hold the address of any object.  In other words, |char*| is a
generic pointer, just like |void*|.

>If that distinction is allowed in the language the cray compiler
>writers can say "No char*'s?  Great, everything is aligned.  Don't even
>bother to check bits 64:62 for an offset in a word."

<chuckle>  What if the |void*| points at the second char in an array?

>No code I write, nor those whom I respect writes code that uses char*'s
>when void*'s are needed.  Nor do they assume sizeof( char[ 1 ] ) == 1
> != sizeof( char[ 2 ] ), etc.

Then you're not programming in ANSI C, which guarantees all the things
you write above.  No wonder you have so many misconceptions about the
language.
-- 
Chip Salzenberg at Teltronics/TCT     <chip@tct.uucp>, <uunet!pdn!tct!chip>
 "Most of my code is written by myself.  That is why so little gets done."
                 -- Herman "HLLs will never fly" Rubin
Technical Details
Message-ID: <27DBCE85.5865@tct.uucp>
Path: arc1-ca.pugleaf.net!utzoo!news-server.csri.toronto.edu!rutgers!usc!samsung!spool.mu.edu!uunet!pdn!tscs!tct!chip
References: <3568@ux.acs.umn.edu> <27D8437B.F13@tct.uucp> <3583@ux.acs.umn.edu>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!news-server.csri.toronto.edu!rutgers!usc!samsung!spool.mu.edu!uunet!pdn!tscs!tct!chip
From: chip@tct.uucp (Chip Salzenberg)
Newsgroups: comp.std.c++
Subject: Re: Conversions to/from void*, redux
Keywords: Sun, C++
Message-ID: <27DBCE85.5865@tct.uucp>
Date: 11 Mar 91 18:37:56 GMT
References: <3568@ux.acs.umn.edu> <27D8437B.F13@tct.uucp> <3583@ux.acs.umn.edu>
Organization: Teltronics/TCT, Sarasota, FL
Lines: 78