Connecting Tech Pros Worldwide Forums | Help | Site Map

Storing address of pointer (cross platform)

howa
Guest
 
Posts: n/a
#1: Jan 19 '08
In 32 bit pc, I can write

e.g.

char* p1 = "apple";
cout<<(int)p1;


In 64 bit pc, I need to use

char* p1 = "apple";
cout<<(__int64)p1;


What are the cross platform way to so?

Thanks.



=?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=
Guest
 
Posts: n/a
#2: Jan 19 '08

re: Storing address of pointer (cross platform)


On 2008-01-19 13:47, howa wrote:
Quote:
In 32 bit pc, I can write
>
e.g.
>
char* p1 = "apple";
cout<<(int)p1;
>
>
In 64 bit pc, I need to use
>
char* p1 = "apple";
cout<<(__int64)p1;
>
>
What are the cross platform way to so?
If your platforms have C compilers providing <stdint.hyou can check if
they define the typedef intptr_r or uintptr_t, they should be large
enough for a void pointer. If you do not have those typedefs you need to
provide them (or similar) on your own.

--
Erik Wikström
howa
Guest
 
Posts: n/a
#3: Jan 19 '08

re: Storing address of pointer (cross platform)


On 1月19日, 下午9時12分, Erik Wikström <Erik-wikst...@telia.comwrote:
Quote:
On 2008-01-19 13:47, howa wrote:
>
Quote:
In 32 bit pc, I can write
>
Quote:
e.g.
>
Quote:
char* p1 = "apple";
cout<<(int)p1;
>
Quote:
In 64 bit pc, I need to use
>
Quote:
char* p1 = "apple";
cout<<(__int64)p1;
>
Quote:
What are the cross platform way to so?
>
If your platforms have C compilers providing <stdint.hyou can check if
they define the typedef intptr_r or uintptr_t, they should be large
enough for a void pointer. If you do not have those typedefs you need to
provide them (or similar) on your own.
>
--
Erik Wikström
Thanks.
=?iso-8859-1?q?Tom=E1s_=D3_h=C9ilidhe?=
Guest
 
Posts: n/a
#4: Jan 19 '08

re: Storing address of pointer (cross platform)


howa:
Quote:
In 32 bit pc, I can write
>
e.g.
>
char* p1 = "apple";
cout<<(int)p1;
>
>
In 64 bit pc, I need to use
>
char* p1 = "apple";
cout<<(__int64)p1;
>
>
What are the cross platform way to so?

I'm not sure if cout can print a pointer, but printf certainly can:

int i;

printf("%p",(void*)&i);

--
Toms hilidhe
Rolf Magnus
Guest
 
Posts: n/a
#5: Jan 19 '08

re: Storing address of pointer (cross platform)


howa wrote:
Quote:
In 32 bit pc, I can write
>
e.g.
>
char* p1 = "apple";
cout<<(int)p1;
>
>
In 64 bit pc, I need to use
>
char* p1 = "apple";
cout<<(__int64)p1;
>
>
What are the cross platform way to so?
cout << static_cast<void*>(p1);

James Kanze
Guest
 
Posts: n/a
#6: Jan 20 '08

re: Storing address of pointer (cross platform)


On Jan 19, 1:47 pm, howa <howac...@gmail.comwrote:
Quote:
In 32 bit pc, I can write
Quote:
e.g.
Quote:
char* p1 = "apple";
cout<<(int)p1;
Quote:
In 64 bit pc, I need to use
Quote:
char* p1 = "apple";
cout<<(__int64)p1;
(A more portable 64 bit type would be long long.)
Quote:
What are the cross platform way to so?
cout << static_cast< void* >( p1 ) ;

It's the only way guaranteed to work everywhere.

It doesn't allow you to control the output format in any way,
however. If you need to control the output format, something
like:
cout << static_cast< uintptr_t >( p1 ) ;
will usually work (but uintptr_t is not guaranteed to be present
on a machine where pointers are larger than 64 bits).

If you have to work with an older compiler, which doesn't
support the new integer types, then casting to size_t is often
OK (although I've used platforms where it wouldn't work).

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique oriente objet/
Beratung in objektorientierter Datenverarbeitung
9 place Smard, 78210 St.-Cyr-l'cole, France, +33 (0)1 30 23 00 34
Closed Thread


Similar C / C++ bytes