Re: addresses and integers

From: Dan Pop (Dan.Pop_at_cern.ch)
Date: 08/31/04


Date: 31 Aug 2004 12:28:38 GMT

In <8c7d4a6e.0408300702.368efb7c@posting.google.com> junky_fellow@yahoo.co.in (junky_fellow) writes:

>why the conversion of a pointer type variable to integer invalid ?

The standard says that it's valid (with one exception) and that the
result is implementation-defined.

>what's the reason behind that ?

It may be possible (AS/400 springs to mind) that no integer type is
wide enough to hold the result of the conversion. This is the exception
mentioned above.

As for the implementation-defined result, there are architectures, like
the 8086, where the pointer value is not the same as the address pointed
to and most addresses can have 4096 different representations as pointer
values.

>i always had in my mind that pointer variable contains some address,
>which is some integer value ?

In some cases, see above, it may be more than one integer value. The
actual address is computed by the CPU itself, from these numbers.

>and i can add/subtract after typecasting the pointer variable to int.

You can do that, but the result need not have the desired/expected
meaning. You have to understand how the conversion works on a given
platform (the implementation must document it) in order to do this kind
of things in a meaningful way. Which means that the code cannot be
expect to work as intended on another implementation.

Dan

-- 
Dan Pop
DESY Zeuthen, RZ group
Email: Dan.Pop@ifh.de


Relevant Pages

  • Re: size of a sizeof(pointer)
    ... snip ... ... >> main exception to the rule that the size of the pointer represents ... >pointer) or used a 32 bit pointer. ... Dan Pop ...
    (comp.lang.c)
  • Re: size of a sizeof(pointer)
    ... snip ... ... >> main exception to the rule that the size of the pointer represents ... >pointer) or used a 32 bit pointer. ... Dan Pop ...
    (comp.lang.c)
  • Re: why do strcpy, strcat, & co return a pointer ?
    ... >meaning of my original claim should've been clear from the context, ... >IG> a pointer to its first element. ... Dan Pop ...
    (comp.lang.c)
  • Re: Tlb Load Exception!
    ... Exception and reboot because of that exception. ... architecture and doing an access there always causes a trap. ... on PPC, the exception vector table lives in page 0, so it is mapped. ... However this does not mean that a NULL pointer dereference is not a ...
    (comp.os.vxworks)
  • Re: c++ documentation
    ... >> mapping of the language onto assembly for some architecture. ... catastrophes that come from pointer misuse: ... >> types of smart pointer to use and which behavior it should have. ... > indirectly) that can throw an exception has to be written so that it ...
    (comp.os.linux.development.apps)