Re: Converting a pointer to a long

Discussion in 'C++' started by Vaclav Haisman, May 16, 2010.

  1. Giuliano Bertoletti wrote, On 16.5.2010 21:17:
    >
    > Hello,
    >
    > how do I convert to an interger (or long) the address a pointer is
    > pointing to:
    >
    > If I try:
    >
    > const unsigned char *p = ...
    > const unsigned long j = static_cast<const unsigned long>(p);

    Don't do that if you can avoid it. It can be that sizeof (unsigned long) <
    sizeof (unsigned char *) and your code will be doomed if you ever try to cast
    back to the pointer. If you really really cannot do without it then use at
    least std::size_t or std::ptrdiff_t types to hold the pointer as integer.

    >
    > the compiler issue:
    >
    > error C2440: 'static_cast' : cannot convert from 'const unsigned char *'
    > to 'unsigned long'

    Use reinterpret_cast<>.

    --
    VH
     
    Vaclav Haisman, May 16, 2010
    #1
    1. Advertising

  2. Vaclav Haisman

    Kai-Uwe Bux Guest

    Vaclav Haisman wrote:

    > Giuliano Bertoletti wrote, On 16.5.2010 21:17:
    >>
    >> Hello,
    >>
    >> how do I convert to an interger (or long) the address a pointer is
    >> pointing to:
    >>
    >> If I try:
    >>
    >> const unsigned char *p = ...
    >> const unsigned long j = static_cast<const unsigned long>(p);

    > Don't do that if you can avoid it. It can be that sizeof (unsigned long) <
    > sizeof (unsigned char *) and your code will be doomed if you ever try to
    > cast back to the pointer. If you really really cannot do without it then
    > use at least std::size_t or std::ptrdiff_t types to hold the pointer as
    > integer.

    [...]

    Just a nit: the standard does not guarantee that std::size_t or
    std::ptrdiff_t can hold an address. In fact, the standard does not guarantee
    the existence of an integer type large enough.

    With C++0X, one could use std::intptr_t, which makes these guarantees. The
    only drawback is, that as of draft n3092 this type is optional :-(


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, May 16, 2010
    #2
    1. Advertising

  3. Kai-Uwe Bux wrote, On 16.5.2010 22:30:
    > Vaclav Haisman wrote:
    >
    >> Giuliano Bertoletti wrote, On 16.5.2010 21:17:
    >>>
    >>> Hello,
    >>>
    >>> how do I convert to an interger (or long) the address a pointer is
    >>> pointing to:
    >>>
    >>> If I try:
    >>>
    >>> const unsigned char *p = ...
    >>> const unsigned long j = static_cast<const unsigned long>(p);

    >> Don't do that if you can avoid it. It can be that sizeof (unsigned long) <
    >> sizeof (unsigned char *) and your code will be doomed if you ever try to
    >> cast back to the pointer. If you really really cannot do without it then
    >> use at least std::size_t or std::ptrdiff_t types to hold the pointer as
    >> integer.

    > [...]
    >
    > Just a nit: the standard does not guarantee that std::size_t or
    > std::ptrdiff_t can hold an address. In fact, the standard does not guarantee
    > the existence of an integer type large enough.

    True but in practice both types are wide enough. And some asserts, static or
    classic, can catch the situations where the types are really not wide enough.

    >
    > With C++0X, one could use std::intptr_t, which makes these guarantees. The
    > only drawback is, that as of draft n3092 this type is optional :-(


    --
    VH
     
    Vaclav Haisman, May 17, 2010
    #3
  4. Vaclav Haisman

    Öö Tiib Guest

    On May 16, 11:30 pm, Kai-Uwe Bux <> wrote:
    > Just a nit: the standard does not guarantee that std::size_t or
    > std::ptrdiff_t can hold an address. In fact, the standard does not guarantee
    > the existence of an integer type large enough.


    I always suggest to put BOOST_STATIC_ASSERT() into compilation units
    that assume something.

    Fr example put BOOST_STATIC_ASSERT(sizeof(short) == 2) to where code
    assumes that short is 2 bytes. Then it is not assumed anymore but
    granted, otherwise it won't compile. ;)
     
    Öö Tiib, May 17, 2010
    #4
  5. Vaclav Haisman

    James Kanze Guest

    On 16 May, 21:30, Kai-Uwe Bux <> wrote:
    > Vaclav Haisman wrote:
    > > Giuliano Bertoletti wrote, On 16.5.2010 21:17:


    > >> how do I convert to an interger (or long) the address a pointer is
    > >> pointing to:


    > >> If I try:

    >
    > >> const unsigned char *p = ...
    > >> const unsigned long j = static_cast<const unsigned long>(p);

    > > Don't do that if you can avoid it. It can be that sizeof (unsigned long)
    > > < sizeof (unsigned char *) and your code will be doomed if you ever try to
    > > cast back to the pointer. If you really really cannot do without it then
    > > use at least std::size_t or std::ptrdiff_t types to hold the pointer as
    > > integer.


    > [...]


    > Just a nit: the standard does not guarantee that std::size_t
    > or std::ptrdiff_t can hold an address. In fact, the standard
    > does not guarantee the existence of an integer type large
    > enough.


    > With C++0X, one could use std::intptr_t, which makes these
    > guarantees. The only drawback is, that as of draft n3092 this
    > type is optional :-(


    As it is in C, too, I believe. The implementation is only
    required to provide the typedef if it has an integral type which
    meets the requirements.

    In practice: the next C++ standard (and the current C standard)
    require an integral type of at least 64 bits. And I don't
    expect to see machines which require more than 64 bit addresses
    for a long, long time. So you're probably safe.

    --
    James Kanze
     
    James Kanze, May 18, 2010
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. George Marsaglia

    Assigning unsigned long to unsigned long long

    George Marsaglia, Jul 8, 2003, in forum: C Programming
    Replies:
    1
    Views:
    691
    Eric Sosman
    Jul 8, 2003
  2. silangdon

    Converting long long to char

    silangdon, Mar 21, 2005, in forum: C Programming
    Replies:
    8
    Views:
    405
    Richard Bos
    Mar 22, 2005
  3. Daniel Rudy

    unsigned long long int to long double

    Daniel Rudy, Sep 19, 2005, in forum: C Programming
    Replies:
    5
    Views:
    1,205
    Peter Shaggy Haywood
    Sep 20, 2005
  4. Mathieu Dutour

    long long and long

    Mathieu Dutour, Jul 17, 2007, in forum: C Programming
    Replies:
    4
    Views:
    485
    santosh
    Jul 24, 2007
  5. Bart C

    Use of Long and Long Long

    Bart C, Jan 9, 2008, in forum: C Programming
    Replies:
    27
    Views:
    814
    Peter Nilsson
    Jan 15, 2008
Loading...

Share This Page