How to cast an specified type pointer to integer?

A

Allen

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?
 
I

Ian Collins

Allen said:
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?
 
A

Allen

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


Yes.
I package RPC output parameter to a byte buffer. So I need to remember
the output variable address.
How to do it?
 
O

Ondra Holub

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.
 
A

Allen

Allen napsal:



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.
 
O

Ondra Holub

Allen napsal:
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.
 
J

Jacek Dziedzic

Allen said:
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.
 
D

Dave Rahardja

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
 
I

Ivan Novick

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
 
S

Sylvester Hesp

Ivan Novick said:
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
 
S

Sylvester Hesp

Sylvester Hesp said:
-----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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top