Re: Ada Pointer Size Problem

From: Dave Thompson (david.thompson1_at_worldnet.att.net)
Date: 11/01/04


Date: Mon, 01 Nov 2004 08:14:03 GMT

On Thu, 21 Oct 2004 01:56:28 GMT, Jeffrey Carter <spam@spam.com>
wrote:

> Dave Thompson wrote:
>
> > On Mon, 11 Oct 2004 05:18:58 GMT, Jeffrey Carter <spam@spam.com>
> > wrote:
> >>
> >> In C/++, a pointer is an int is an address. In Ada, they're 3
> >> different things.
> >
> > But (unlike its predecessor B) a C pointer/address is not just an
> > int.
>
> I meant that a pointer is an int because you can always convert/assign a
> pointer to an int.

Well, in many languages (not just C) you can just assign an integer
(or fixed-point) value to floating-point and vice versa, but I sure
wouldn't say that either "is" the other.

And you can't assign C pointer to integer without conversion; it
violates a constraint. (Or pass as parameter or return as value, since
in C those are always semantically equivalent to assignment.)

You can write the conversion to any integer type explicitly with a
cast, but the result is implementation-defined and may or may not be
useful. For most mainstream systems there is a simple and useful
mapping and that is what any sensible implementation provides, but the
Standard doesn't require it.

As others have already said, the specific type 'int', which is only
one of the integer types provided by a conforming C, need not be large
enough to handle (all) addresses. In addition to the 286 example, I
have long personal experience of the "classic" (that is, pre-RISC,
still used in emulation) Tandem^WCompaq^WHP NonStop, with three
execution models: IP16, I16P32, and IP32; corresponding basically to
different stages in their CPU/ISA evolution. In the "large" I16P32
model obviously pointer in int doesn't work.

The new-in-C99 names intptr_t and uintptr_t are integer types
guaranteed large enough to losslessly store (with the explicit
conversion) any (data) pointer, but are optional; an implementation
need not provide them if it doesn't have a suitable integer type. As
with all the new <stdint.h> types, there is an associated preprocessor
macro you can test to determine if the type is present, and thus write
code that works-around the omission or at least fails gracefully --
but only with the ugliness typical of preprocessor solutions, as often
discussed here. But machines that actually can't do address arithmetic
-- and thus reasonably support such an integer type -- are for obvious
reasons pretty damn rare if they exist at all, and also (would be)
very unlikely to be able to support the rest of C anyway.

- David.Thompson1 at worldnet.att.net



Relevant Pages

  • Re: conversions
    ... than or equal to the rank of int and unsigned int. ... supported by an implementation that have lower conversion rank than int. ... "The rank of any unsigned integer type shall equal the rank of the corresponding signed integer type, ...
    (comp.lang.c)
  • Re: Common Problems that Compilers Dont Catch
    ... where the compiler offered no help. ... Become more familiar with pointer variables and you won't be making such ... int i = p; ... Any pointer type may be converted to an integer type. ...
    (comp.lang.c)
  • Re: typedef function with void parameters
    ... void pointer. ... | int foo ... data pointer type, but you need to know what the other type is ... in order to choose the proper conversion. ...
    (comp.lang.c)
  • Re: pointer question
    ... p = (char*) malloc; ... Without a declaration for malloc in scope, C assumes that it returns an int. ... If a void* cannot be represented in an int, then the behavior of the first conversion is undefined. ... And when you have converted some T* to the appropriate integer, the only kind of conversion back to pointer which is guaranteed to work is to T*. ...
    (comp.lang.c)
  • Re: Minimum size of void *
    ... Flash Gordon wrote: ... converted losslessly to int. ... The pointer to the global section of the function, ... Keith was talking about whether they could be converted, the conversion being a common extension and gave size as one possible reason why it might not be possible. ...
    (comp.lang.c)