🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c.moderated
Article #2519

Re: void* type

#2519
From: Richard A. O'Keefe
Date: Fri, 05 Apr 1996 00:51
56 lines
2178 bytes
davis@space.mit.edu (John E. Davis) writes:

>So, does this fail:

>   double *d;
>   char *p;
>   unsigned int size;
>   
>   size = sizeof (double);
>   d = malloc (size);
>   p = malloc (size);

No.

>What is the difference between d and p?

Well, to start with, they must point to different objects.  (:-)

But, to take a real example, 
	d might be represented as
		indirect flag in sign bit
		31 bit "word" address, where a "word" is 16 bits
	p might be represented as
		31 bit "word" address (starting in sign bit)
		1 bit byte-within-word offset

>Is malloc smart enough to know which representation to return?

malloc() _always_ returns a void*.  It doesn't know, can't tell, and
doesn't care, what you assign that to.  It is the compiler-generated
code "convert-void*-to-double*" and "convert-void*-to-char*" that is
responsible for changing the representations.

For example, you might get
	LDW	%A,=8
	STW	%A,size
	PUSHW	%A
	CALL	malloc,1
	ROTR	%A		; right rotate		; cast void*
	TRAPN			; trap if sign bit set	; to double*
	STW	%A,d

	LDW	%A,size
	PUSHW	%A
	CALL	malloc,1
	STW	%A,p		; void* and char* have same representation

(The assembler here is imaginary, the encoding most certainly is not.)

Then I could tell you about the machine I used where double* pointers were
32 bits and char* pointers were 48 (and had to be loaded into one of two
very special registers).

-- 
Result of state election = 50% Victorians didn't get MP of their choice.
Richard A. O'Keefe; http://www.cs.rmit.edu.au/~ok; RMIT Comp.Sci.
Technical Details
Message-ID: <4k1qq7$5pc@solutions.solon.com>
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail
References: <4ii34g$nlp@solutions.solon.com> <4jibl5$89u@solutions.solon.com> <4jmcnh$lqk@solutions.solon.com> <4jq24r$aah@solutions.solon.com> <4jv7ge$d49@solutions.solon.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail
From: ok@cs.rmit.edu.au (Richard A. O'Keefe)
Newsgroups: comp.lang.c.moderated
Subject: Re: void* type
Date: 4 Apr 1996 18:51:19 -0600
Organization: Comp Sci, RMIT, Melbourne, Australia
Sender: clc@solutions.solon.com
Approved: clc@solutions.solon.com
Message-ID: <4k1qq7$5pc@solutions.solon.com>
References: <4ii34g$nlp@solutions.solon.com> <4jibl5$89u@solutions.solon.com> <4jmcnh$lqk@solutions.solon.com> <4jq24r$aah@solutions.solon.com> <4jv7ge$d49@solutions.solon.com>
NNTP-Posting-Host: solutions.solon.com
X-Newsreader: NN version 6.5.0 #0 (NOV)