[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: ptrdiff_t misuse

From: Stephen J. Turnbull
Subject: Re: ptrdiff_t misuse
Date: Fri, 06 Jul 2012 19:19:57 +0900

Eli Zaretskii writes:

 > My next issue with ptrdiff_t use is with variables that store buffer
 > or string positions.  They were lately converted from EMACS_INT to
 > ptrdiff_t.  Here're 2 examples:

 > I understand that this is done because these variables are indices
 > into arrays, and thus cannot hold values that the ptrdiff_t type
 > cannot express.  Therefore, EMACS_INT is not good here, because it can
 > be wider than ptrdiff_t.
 > If this is indeed the reason, I suggest a new typedef:
 >   typedef ptrdiff_t EMACS_POS;
 > to be used with all such variables.  I think this will contribute to
 > clarity of the code,

FWIW, as an occasional reader of Emacs code, I disagree.  AIUI,
EMACS_INT exists in principle to express the idea of "C implementation
of the integer type used to implement Lisp objects" (which for some
strange reason ISO/IEC JTC1/SC22/WG14 hasn't standardized, we really
should get them to fix that! ;-), and in practice because of the
horribly inconsistent treatment of integer types across platforms, and
the fact that often Emacs will prefer a wider type to a more efficient
one (in the compiler implementer's opinion).

OTOH, ptrdiff_t expresses exactly the type required by the hardware.
I don't see how you can get more clear than that.

 > since ptrdiff_t is a rarely-used type,

EMACS_INT of course is even more rarely-used. ;-)

 > and thus I expect many programmers to be unfamiliar with it.

So teach them, just as you have to teach them EMACS_INT.  The type
name is mnemonic, as are the variables it describes.  Google brings up
plenty of relevant references.  I don't see a problem.

 > By contrast, EMACS_POS by its very name tells what the variable can
 > and cannot hold.

FWIW, to me it doesn't.  It tells me what role it plays in Emacs, it
doesn't tell me anything about the actual type, or why it needs to be
different from EMACS_INT.  I think it will be very easy for people to
confound EMACS_INT with EMACS_POS, even if they're read the typedef,
because in Lisp both kinds of value are represented by the same type.
OTOH, once a hacker learns the rationale for ptrdiff_t, I think it
will be difficult to forget.

The real problem is that lazy programmers will frequently use a
generic type (such as "int") for variables, because they can't
remember the right type.  To me, "EMACS_POS" is only marginally more
memorable than "ptrdiff_t" for this purpose, and that is more than
outweighed by the precision of "ptrdiff_t" for expressing a hardware-
dependent type.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]