How to cast an specified type pointer to integer?

Discussion in 'C++' started by Allen, Jan 30, 2007.

  1. Allen

    Allen Guest

    I want to package an address to byte buffer. So I need to cast it to
    integer value.
    How to cast it?

    const char * ptr = 0x0013e328;
    int value = <cast> (ptr); // use reinterpret_cast?
     
    Allen, Jan 30, 2007
    #1
    1. Advertising

  2. Allen

    Ian Collins Guest

    Allen wrote:
    > I want to package an address to byte buffer. So I need to cast it to
    > integer value.


    Why? Surely if you are putting it in a byte buffer, you want a pointer?

    --
    Ian Collins.
     
    Ian Collins, Jan 30, 2007
    #2
    1. Advertising

  3. Allen

    Allen Guest

    On 1ÔÂ30ÈÕ, ÏÂÎç4ʱ12·Ö, Ian Collins <> wrote:
    > Allen wrote:
    > > I want to package an address to byte buffer. So I need to cast it to
    > > integer value.

    >
    > Why? Surely if you are putting it in a byte buffer, you want a pointer?
    >
    > --
    > Ian Collins.



    Yes.
    I package RPC output parameter to a byte buffer. So I need to remember
    the output variable address.
    How to do it?
     
    Allen, Jan 30, 2007
    #3
  4. Allen

    Ondra Holub Guest

    Allen napsal:
    > I want to package an address to byte buffer. So I need to cast it to
    > integer value.
    > How to cast it?
    >
    > const char * ptr = 0x0013e328;
    > int value = <cast> (ptr); // use reinterpret_cast?


    Yes, use reinterpret_cast. Or you can use union

    union Cast
    {
    const char* szptr;
    const int* iptr;
    };

    In both cases are you taking the responsibility for this conversion
    (you claim you know how is it stored there). But for casting pointers
    there should be no problem.
     
    Ondra Holub, Jan 30, 2007
    #4
  5. Allen

    Allen Guest

    On 1ÔÂ30ÈÕ, ÏÂÎç4ʱ18·Ö, "Ondra Holub" <> wrote:
    > Allen napsal:
    >
    > > I want to package an address to byte buffer. So I need to cast it to
    > > integer value.
    > > How to cast it?

    >
    > > const char * ptr = 0x0013e328;
    > > int value = <cast> (ptr); // use reinterpret_cast?

    >
    > Yes, use reinterpret_cast. Or you can use union
    >
    > union Cast
    > {
    > const char* szptr;
    > const int* iptr;
    >
    > };
    >
    > In both cases are you taking the responsibility for this conversion
    > (you claim you know how is it stored there). But for casting pointers
    > there should be no problem.


    I do like this:
    int address =
    reinterpret_cast<int>(reinterpret_cast<int*>(const_cast<char*>((ptr))))
    ;

    But the compiler gives a warning, warning C4311: "reinterpret_cast":
    from int* to int pointer cut off.
     
    Allen, Jan 30, 2007
    #5
  6. Allen

    Ondra Holub Guest

    Allen napsal:
    > On 1ÔÂ30ÈÕ, ÏÂÎç4ʱ18·Ö, "Ondra Holub" <> wrote:
    > > Allen napsal:
    > >
    > > > I want to package an address to byte buffer. So I need to cast it to
    > > > integer value.
    > > > How to cast it?

    > >
    > > > const char * ptr = 0x0013e328;
    > > > int value = <cast> (ptr); // use reinterpret_cast?

    > >
    > > Yes, use reinterpret_cast. Or you can use union
    > >
    > > union Cast
    > > {
    > > const char* szptr;
    > > const int* iptr;
    > >
    > > };
    > >
    > > In both cases are you taking the responsibility for this conversion
    > > (you claim you know how is it stored there). But for casting pointers
    > > there should be no problem.

    >
    > I do like this:
    > int address =
    > reinterpret_cast<int>(reinterpret_cast<int*>(const_cast<char*>((ptr))))
    > ;
    >
    > But the compiler gives a warning, warning C4311: "reinterpret_cast":
    > from int* to int pointer cut off.


    I see you are trying to assign pointer value to int. It is not good
    idea. It is quite common, that pointer does not fit into int value
    (for example on 64-bits systems). That's the reason for such warning.
     
    Ondra Holub, Jan 30, 2007
    #6
  7. Allen wrote:
    > I want to package an address to byte buffer. So I need to cast it to
    > integer value.


    Why an integer? What if a pointer does not fit into
    an integer? On my system
    sizeof(void*)==8, sizeof(int)==4, what then?

    I would try pointing a char* pointer to the
    pointer in question (not to the address!) and
    then read subsequent char's, incrementing the
    pointer and storing them into a vector<char>
    that would be sizeof(void*) elements long.

    HTH,
    - J.
     
    Jacek Dziedzic, Jan 30, 2007
    #7
  8. On Tue, 30 Jan 2007 09:54:30 +0100, Jacek Dziedzic
    <> wrote:

    >Allen wrote:
    >> I want to package an address to byte buffer. So I need to cast it to
    >> integer value.

    >
    > Why an integer? What if a pointer does not fit into
    >an integer? On my system
    >sizeof(void*)==8, sizeof(int)==4, what then?
    >
    > I would try pointing a char* pointer to the
    >pointer in question (not to the address!) and
    >then read subsequent char's, incrementing the
    >pointer and storing them into a vector<char>
    >that would be sizeof(void*) elements long.


    I suspect the OP is dealing with some I/O protocol that specifies a certain
    number of bytes for pointer types, regardless of platform.

    OP: This is very low level code and you'd better know what you're doing. It
    helps to do the following as well:

    #include "boost/static_assert.hpp"

    BOOST_STATIC_ASSERT(sizeof(void*) <= sizeof(int));

    See boost.org for the boost library.

    -dr
     
    Dave Rahardja, Jan 30, 2007
    #8
  9. Allen

    Ivan Novick Guest

    On Jan 30, 12:29 am, "Allen" <> wrote:
    > I do like this:
    > int address =
    > reinterpret_cast<int>(reinterpret_cast<int*>(const_cast<char*>((ptr))))
    > ;
    >
    > But the compiler gives a warning, warning C4311: "reinterpret_cast":
    > from int* to int pointer cut off.- Hide quoted text -


    Yes, like the other poster said, the sizeof int on your system may not
    be big enough to hold a pointer value. reinterpret_cast is definitely
    the right way to cast it. Instead of putting the result into an int,
    you can use intptr_t which is guaranteed to be a typedef to an integer
    type big enough to hold a pointer on your system. intptr_t is not
    part of the C++ standard, but I believe it is part of the C standard,
    and is available in g++, and may even be in the queue to be added to
    the C++ standard in the future.

    Ivan
    http://www.0x4849.net
     
    Ivan Novick, Jan 30, 2007
    #9
  10. "Ivan Novick" <> wrote in message
    news:...
    > On Jan 30, 12:29 am, "Allen" <> wrote:
    >> I do like this:
    >> int address =
    >> reinterpret_cast<int>(reinterpret_cast<int*>(const_cast<char*>((ptr))))
    >> ;
    >>
    >> But the compiler gives a warning, warning C4311: "reinterpret_cast":
    >> from int* to int pointer cut off.- Hide quoted text -

    >
    > Yes, like the other poster said, the sizeof int on your system may not
    > be big enough to hold a pointer value. reinterpret_cast is definitely
    > the right way to cast it. Instead of putting the result into an int,
    > you can use intptr_t which is guaranteed to be a typedef to an integer
    > type big enough to hold a pointer on your system. intptr_t is not
    > part of the C++ standard, but I believe it is part of the C standard,
    > and is available in g++, and may even be in the queue to be added to
    > the C++ standard in the future.
    >
    > Ivan
    > http://www.0x4849.net
    >


    -----code-----------------------------------------------
    #include <cstddef>

    template<int> struct intselector;
    template<> struct intselector<1>
    { typedef unsigned char type; };
    template<> struct intselector<2>
    { typedef unsigned short type; };
    template<> struct intselector<3>
    { typedef unsigned int type; };
    template<> struct intselector<4>
    { typedef unsigned long type; };

    // and if your platform supports it (non-standard):
    //template<> struct intselector<5>
    // { typedef unsigned long long type; };



    template<size_t S> struct inttype
    {
    typedef typename intselector
    <
    S <= sizeof(unsigned char)
    ? 1
    : S <= sizeof(unsigned short)
    ? 2
    : S <= sizeof(unsigned int)
    ? 3
    : S <= sizeof(unsigned long)
    ? 4
    : 5
    >::type type;

    };

    typedef inttype<sizeof(void*)>::type intptr_t;
    -----end code-------------------------------------------


    Now you have an intptr_t on every platform ;)

    - Sylvester
     
    Sylvester Hesp, Jan 31, 2007
    #10
  11. "Sylvester Hesp" <> wrote in message
    news:45c07098$0$325$4all.nl...
    > "Ivan Novick" <> wrote in message
    > news:...
    >> On Jan 30, 12:29 am, "Allen" <> wrote:
    >>> I do like this:
    >>> int address =
    >>> reinterpret_cast<int>(reinterpret_cast<int*>(const_cast<char*>((ptr))))
    >>> ;
    >>>
    >>> But the compiler gives a warning, warning C4311: "reinterpret_cast":
    >>> from int* to int pointer cut off.- Hide quoted text -

    >>
    >> Yes, like the other poster said, the sizeof int on your system may not
    >> be big enough to hold a pointer value. reinterpret_cast is definitely
    >> the right way to cast it. Instead of putting the result into an int,
    >> you can use intptr_t which is guaranteed to be a typedef to an integer
    >> type big enough to hold a pointer on your system. intptr_t is not
    >> part of the C++ standard, but I believe it is part of the C standard,
    >> and is available in g++, and may even be in the queue to be added to
    >> the C++ standard in the future.
    >>
    >> Ivan
    >> http://www.0x4849.net
    >>

    >
    > -----code-----------------------------------------------
    > -----end code-------------------------------------------
    >
    >
    > Now you have an intptr_t on every platform ;)
    >
    > - Sylvester


    It has come to my attention that intptr_t is always signed, and the
    uintptr_t is always unsigned (in C99). To adopt this behaviour the changes
    to my code are of course straightforward.

    - Sylvester
     
    Sylvester Hesp, Jan 31, 2007
    #11
    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. .Net Sports

    Specified cast error on an integer datatype

    .Net Sports, Jul 8, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    374
    =?Utf-8?B?Q2hhbmRyYWt1bWFy?=
    Jul 8, 2005
  2. Rocke Robertson
    Replies:
    14
    Views:
    13,004
    Dan Pop
    Oct 2, 2003
  3. Markus Dehmann
    Replies:
    14
    Views:
    930
    Irrwahn Grausewitz
    Jun 28, 2004
  4. Dawn Minnis
    Replies:
    4
    Views:
    1,552
    Dawn Minnis
    Feb 20, 2005
  5. Replies:
    2
    Views:
    1,258
    Martin Ambuhl
    Apr 1, 2005
Loading...

Share This Page