Re: writing library functions and pointers?
- From: "Morris Keesan" <keesan@xxxxxxxxxxx>
- Date: Fri, 10 Jul 2009 02:16:01 GMT
On Thu, 09 Jul 2009 08:17:47 -0400, Ben Bacarisse <ben.usenet@xxxxxxxxx> wrote:
"Morris Keesan" <keesan@xxxxxxxxxxx> writes:
On Mon, 06 Jul 2009 09:47:26 -0400, lovecreatesbeauty@xxxxxxxxx
<lovecreatesbeauty@xxxxxxxxx> wrote:
k&r errata #142:
On the other hand, pre-ANSI, the cast was
necessary [from malloc return to other pointer types]
I've seen this claim many times, but I've never used any compiler for
which the cast was necessary, nor seen any definition of the language
in which it was necessary, including, among others, the language
definition as published in K&R 1, or the (almost identical) C Language
documentation in the version 6 or 7 Unix documentation.
You've had some references, but there are also real examples. I
worked on one machine that was (16-bit) word-addressed. To represent
character pointers, the pointer to the containing word was shifted
left by 1 and the bottom bit was used to pick which byte was being
addressed.
When compiling:
int i;
char *ip = (char *)&i;
double *dp = (double *)malloc(sizeof *dp);
the casts generated code (a shift left and a shift right). You might
wonder why the compiler could not just generate this code without the
cast but remember this is K&R C where there are no function
prototypes.
This has nothing to do with function prototypes. If malloc were
properly declared as returning a (char *), then the
conversion-by-assignment which would be generated by
double *dp = malloc(sizeof *dp);
should be the same conversion generated by the cast, i.e. conversion
of (char *) to (double *). If malloc were not declared, the conversion
would have been the (incorrect) (int) to (double *). Are you saying
that on this word-addressed machine, assigning a (char *) to a (double *)
without a cast did not generate the code to do a type conversion?
I also worked on a word-addressed machine (a Honeywell Level 6), where
(char *) was twice as large as all other pointers, because the C compiler
implemented (char *) as an (int *) plus an extra word indicating high or
low byte. As you say below, some code was a "challenge to port", since
so many people wrote code which assumed that all pointers had identical
representations. But even on that machine, assigning a (char *) to
any other data-pointer type caused the correct conversion to occur
(assuming that the (char *) were properly aligned), just as if the
conversion were done by a cast before the assignment.
Faced with:
use_pointer(malloc(16));
the compiler has no idea what type of pointer the function expects.
Some of the Unix utilities were a challenge to port.
Yes, and in this case, the result of malloc would have to be
converted to the correct pointer type by an explicit cast, but this
is totally different from the case of an assignment, where the
left-hand side of the assignment operator imposes a type which must
be converted to (except in the cases cited by Keith Thompson, for
which thanks, Keith).
.
- Follow-Ups:
- Re: writing library functions and pointers?
- From: Ben Bacarisse
- Re: writing library functions and pointers?
- From: Keith Thompson
- Re: writing library functions and pointers?
- References:
- writing library functions and pointers?
- From: Martin
- Re: writing library functions and pointers?
- From: Barry Schwarz
- Re: writing library functions and pointers?
- From: Martin
- Re: writing library functions and pointers?
- From: Barry Schwarz
- Re: writing library functions and pointers?
- From: Martin
- Re: writing library functions and pointers?
- From: Barry Schwarz
- Re: writing library functions and pointers?
- From: Morris Keesan
- Re: writing library functions and pointers?
- From: Kaz Kylheku
- Re: writing library functions and pointers?
- From: lovecreatesbeauty@xxxxxxxxx
- Re: writing library functions and pointers?
- From: Morris Keesan
- Re: writing library functions and pointers?
- From: Ben Bacarisse
- writing library functions and pointers?
- Prev by Date: Re: [OT] Confusion
- Next by Date: Re: writing library functions and pointers?
- Previous by thread: Re: writing library functions and pointers?
- Next by thread: Re: writing library functions and pointers?
- Index(es):
Relevant Pages
|