[comp.lang.c] Whose code should we break?

cgy@cs.brown.edu (Curtis Yarvin) (02/20/91)

In article <1215@dms.UUCP> albaugh@dms.UUCP (Mike Albaugh) writes:
>From article <MARC.91Feb14085851@marc.watson.ibm.com>, by marc@marc.watson.ibm.com (Marc Auslander):
>> Anyone out there for sizeof int == 4?
>
>	K&R defined 'int' to be the "natural" size for values that have
>no particular size requirement, other than being "big enough" :-) Anyway,
>because some yahoos decided that sizeof(int) == sizeof( char *) a _long_
>time ago, and because Motorola decided making the 68000 and "almost" 32-bit
>machine, and because compiler vendors (perhaps justifiably) decided to make
>sizeof(int) == 4 for the 68K, _I'm_ stuck porting a whole bunch of code
>crawling with "register short i,j,k" to a 32-bit machine, whose compiler
>obligingly does a truncate after every "++i" in a loop :-(

I use "short" for integers on the 68k too.  But "register short"?  That's
human error.  The 68k does, after all, have 32-bit registers.

>	PLEASE DON'T LIE ABOUT THE SIZE OF YOUR INT. You will penalize
>thoughtful competent programmers who thought about portability and growth
>to reward lazy "all the world's a vax" (tm) hackers.

I don't give a damn how big int is; I use int only when I don't care.

But I want sizeof long == sizeof char *.  There are quite a few applications
in which I find myself writing my own memory manager; I need some type
in which I can flick the bits on my pointers, portably.

curtis

"I tried living in the real world
 Instead of a shell
 But I was bored before I even began." - The Smiths

jmaynard@thesis1.hsch.utexas.edu (Jay Maynard) (02/20/91)

In article <65469@brunix.UUCP> cgy@cs.brown.edu (Curtis Yarvin) writes:
>But I want sizeof long == sizeof char *.  There are quite a few applications
>in which I find myself writing my own memory manager; I need some type
>in which I can flick the bits on my pointers, portably.

All the world is not a VAX.

Operating on pointers as though they had no meaning beyond their numeric
value is inherently nonportable, and if you do that, you deserve to lose.

-- 
Jay Maynard, EMT-P, K5ZC, PP-ASEL | Never ascribe to malice that which can
jmaynard@thesis1.hsch.utexas.edu  | adequately be explained by stupidity.
  "You can even run GNUemacs under X-windows without paging if you allow
          about 32MB per user." -- Bill Davidsen  "Oink!" -- me

gwyn@smoke.brl.mil (Doug Gwyn) (02/20/91)

In article <65469@brunix.UUCP> cgy@cs.brown.edu (Curtis Yarvin) writes:
>But I want sizeof long == sizeof char *.  There are quite a few applications
>in which I find myself writing my own memory manager; I need some type
>in which I can flick the bits on my pointers, portably.

As maintainer of several major pieces of software that did just that,
I highly DISrecommend the practice.  If you can find ANY way to let
the C implementation handle memory management sufficiently well for
your actual needs, you are far better off doing so.

Clearly bit diddling in pointers in far from portable.

cgy@cs.brown.edu (Curtis Yarvin) (02/20/91)

In article <65469@brunix.UUCP> cgy@cs.brown.edu (Curtis Yarvin) writes:
>In article <1215@dms.UUCP> albaugh@dms.UUCP (Mike Albaugh) writes:
>>From article <MARC.91Feb14085851@marc.watson.ibm.com>, by marc@marc.watson.ibm.com (Marc Auslander):
>>> Anyone out there for sizeof int == 4?
>
>But I want sizeof long == sizeof char *.  There are quite a few applications
>in which I find myself writing my own memory manager; I need some type
>in which I can flick the bits on my pointers, portably.

Well, this has aroused some comment; I shouldn't have said "portably."  I
should have said "portably to anywhere I know about."  Does anyone know of
a modern machine on which (char *)((unsigned long)("Yow")+1) is not "ow"?
(Forgetting word-addressed machines, like Crays.)  This is a serious
question, not rhetorical.

Curtis

"I tried living in the real world
 Instead of a shell
 But I was bored before I even began." - The Smiths

jbuck@galileo.berkeley.edu (Joe Buck) (02/20/91)

In article <65469@brunix.UUCP>, cgy@cs.brown.edu (Curtis Yarvin) writes:
|> But I want sizeof long == sizeof char *.  There are quite a few applications
|> in which I find myself writing my own memory manager; I need some type
|> in which I can flick the bits on my pointers, portably.

Use size_t.  This is a typedef which is in /usr/include/sys/types.h on
most Unix systems.  ANSI C requires every C implementation to provide
a definition for it.  It must evaluate to an integral type big enough
to hold a pointer.



--
Joe Buck
jbuck@galileo.berkeley.edu	 {uunet,ucbvax}!galileo.berkeley.edu!jbuck	

sef@kithrup.COM (Sean Eric Fagan) (02/20/91)

In article <65527@brunix.UUCP> cgy@cs.brown.edu (Curtis Yarvin) writes:
>Well, this has aroused some comment; I shouldn't have said "portably."  I
>should have said "portably to anywhere I know about."  Does anyone know of
>a modern machine on which (char *)((unsigned long)("Yow")+1) is not "ow"?
>(Forgetting word-addressed machines, like Crays.)  This is a serious
>question, not rhetorical.

Pr1me's, Cybers (180 state, that is), '386s in large model (i.e., multiple
segments), possibly some of hp's risc machines.

-- 
Sean Eric Fagan  | "I made the universe, but please don't blame me for it;
sef@kithrup.COM  |  I had a bellyache at the time."
-----------------+           -- The Turtle (Stephen King, _It_)
Any opinions expressed are my own, and generally unpopular with others.

sef@kithrup.COM (Sean Eric Fagan) (02/20/91)

In article <11285@pasteur.Berkeley.EDU> jbuck@galileo.berkeley.edu (Joe Buck) writes:
>Use size_t.  This is a typedef which is in /usr/include/sys/types.h on
>most Unix systems.  ANSI C requires every C implementation to provide
>a definition for it.  It must evaluate to an integral type big enough
>to hold a pointer.

Right.  Sure.  I see you've actually read anything Doug has ever posted
about the subject.

(that was extreme sarcasm, for those who weren't here when I typed it.)

size_t is an integral type *big enough to hold the size of any single
object*.  On a '286, for example, with a 16Mb address space, size_t should
be int, not long.

-- 
Sean Eric Fagan  | "I made the universe, but please don't blame me for it;
sef@kithrup.COM  |  I had a bellyache at the time."
-----------------+           -- The Turtle (Stephen King, _It_)
Any opinions expressed are my own, and generally unpopular with others.

richard@aiai.ed.ac.uk (Richard Tobin) (02/21/91)

In article <4754@lib.tmc.edu> jmaynard@thesis1.hsch.utexas.edu (Jay Maynard) writes:
>Operating on pointers as though they had no meaning beyond their numeric
>value is inherently nonportable, and if you do that, you deserve to lose.

So everyone who writes a nonportable program deserves to lose?

-- Richard
-- 
Richard Tobin,                       JANET: R.Tobin@uk.ac.ed             
AI Applications Institute,           ARPA:  R.Tobin%uk.ac.ed@nsfnet-relay.ac.uk
Edinburgh University.                UUCP:  ...!ukc!ed.ac.uk!R.Tobin

brnstnd@kramden.acf.nyu.edu (Dan Bernstein) (02/21/91)

In article <1991Feb20.050525.6515@kithrup.COM> sef@kithrup.COM (Sean Eric Fagan) writes:
  [ criticizing the statement that a size_t can hold a pointer safely ]
> size_t is an integral type *big enough to hold the size of any single
> object*.  On a '286, for example, with a 16Mb address space, size_t should
> be int, not long.

Well, no, there's no reason that it should be an int. What's important
is that it *could* be an int, and it doesn't have to be big enough to
hold a pointer.

I find it strange that the standard insisted that some integral type be
big enough for a pointer. That rule is entirely useless without a way to
figure out what the integral type is. Why did ANSI restrict the
implementor without giving the programmer anything in return?

---Dan

phil@ux1.cso.uiuc.edu (Phil Howard KA9WGN) (02/21/91)

gwyn@smoke.brl.mil (Doug Gwyn) writes:

>In article <65469@brunix.UUCP> cgy@cs.brown.edu (Curtis Yarvin) writes:
>>But I want sizeof long == sizeof char *.  There are quite a few applications
>>in which I find myself writing my own memory manager; I need some type
>>in which I can flick the bits on my pointers, portably.

>As maintainer of several major pieces of software that did just that,
>I highly DISrecommend the practice.  If you can find ANY way to let
>the C implementation handle memory management sufficiently well for
>your actual needs, you are far better off doing so.

>Clearly bit diddling in pointers in far from portable.

Maybe.  Maybe not.  There is one type of data structure in which bit twiddling
will work on every type of pointer I can think of.  Only machine types in which
the size of the pointer itself can vary would have a problem and I know of NO
such machine type.

This data structure is use when you need a doubly-linked list whose elements
are very small and you have a tight memory constraint.  You can construct a
doubly-linked list using ONLY ONE pointer per element (and TWO anchors are
required) by storing the EXCLUSIVE OR of the elements on each side of an
element in that element.

Operating this list is very different.  When you have the pointer to TWO
ADJACENT elements (such as the two anchors as starting point) (I will call
these A and B) you can obtain the pointer to the NEXT element (I will call
it C, not to be confused with any language) by taking the exclusive or of
the pointer to A and the pointer IN B.

One of the interesting things about this approach to scanning through a list
is that if you reverse the starting pointers, the scan will take place in the
opposite direction even though the same exact code is used.  This list is
this symmetrical (which is consistent with there being one pointer item in
each element and the symmetry of exclusive or).

I have had one person tell me that this kind of data structure was too hard
to deal with and non-obvious.  It may not be the most obvious thing around
but once one understand how it works then it would be very clear.  He
complained he would not recognize it when encountering it in code in the
future.  I told him that properly documented code would explain what is
being done and that since he now knows about it, he will recognize it.
I am mentioning this to preempt a bunch of flames about an idea they don't
like.

If you take a pointer, exclusive or it with any bit pattern (usually of the
same size) then later exclusive or it with the SAME bit pattern, you will
get your pointer back.  Is there a machine type in which this is not so?
-- 

--Phil Howard, KA9WGN-- | Individual CHOICE is fundamental to a free society
<phil@ux1.cso.uiuc.edu> | no matter what the particular issue is all about.

gwyn@smoke.brl.mil (Doug Gwyn) (02/21/91)

In article <5768:Feb2020:29:0091@kramden.acf.nyu.edu> brnstnd@kramden.acf.nyu.edu (Dan Bernstein) writes:
>I find it strange that the standard insisted that some integral type be
>big enough for a pointer. That rule is entirely useless without a way to
>figure out what the integral type is.

No, but it does mean that strictly conforming programs should not make
assumptions here.  If you want to print a pointer value as an integer
rather than using %p, I suggest casting it to long (or unsigned long).

%p is better, if you can assume that you have a conforming implementation.

For generic pointer arithmetic, char* is better than any integral type.

>Why did ANSI restrict the
>implementor without giving the programmer anything in return?

Many people wanted such a guarantee.  I agree that it isn't all that
useful in practice.

meissner@osf.org (Michael Meissner) (02/22/91)

In article <1991Feb20.205545.23958@ux1.cso.uiuc.edu>
phil@ux1.cso.uiuc.edu (Phil Howard KA9WGN) writes:

| gwyn@smoke.brl.mil (Doug Gwyn) writes:
| 
| >In article <65469@brunix.UUCP> cgy@cs.brown.edu (Curtis Yarvin) writes:
| >>But I want sizeof long == sizeof char *.  There are quite a few applications
| >>in which I find myself writing my own memory manager; I need some type
| >>in which I can flick the bits on my pointers, portably.
| 
| >As maintainer of several major pieces of software that did just that,
| >I highly DISrecommend the practice.  If you can find ANY way to let
| >the C implementation handle memory management sufficiently well for
| >your actual needs, you are far better off doing so.
| 
| >Clearly bit diddling in pointers in far from portable.
| 
| Maybe.  Maybe not.  There is one type of data structure in which bit twiddling
| will work on every type of pointer I can think of.  Only machine types in which
| the size of the pointer itself can vary would have a problem and I know of NO
| such machine type.

Sigh.....

On PR1ME machines (the 50 series), word pointers were 32 bits, and
byte pointers were 48 bits, and COULD NOT fit in an integer or long.

On the Data General MV computers, word pointers were 32 bits, as were
character pointers, but the format of the two were not the same.  In
particular, the word pointer format was the byte pointer shifted right
1 bit position.  The DG MV also had two types of bit pointers, but
these were not in general supported by the compiler.
--
Michael Meissner	email: meissner@osf.org		phone: 617-621-8861
Open Software Foundation, 11 Cambridge Center, Cambridge, MA, 02142

Considering the flames and intolerance, shouldn't USENET be spelled ABUSENET?