473,396 Members | 1,683 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Storing address of pointer (cross platform)

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.
Jan 19 '08 #1
5 2081
On 2008-01-19 13:47, howa wrote:
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
Jan 19 '08 #2
On 1月19日, 下午9時12分, Erik Wikström <Erik-wikst...@telia.comwrote:
On 2008-01-19 13:47, howa wrote:
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
Thanks.
Jan 19 '08 #3
howa:
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
Jan 19 '08 #4
howa wrote:
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);

Jan 19 '08 #5
On Jan 19, 1:47 pm, howa <howac...@gmail.comwrote:
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;
(A more portable 64 bit type would be long long.)
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:ja*********@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
Jan 20 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: ypjofficial | last post by:
Hello All, While readling the C and C++ literature i came to know that the OS allocates separate protected memory space for each running program and the program do not interfare into one another's...
22
by: Wynand Winterbach | last post by:
I think every C programmer can relate to the frustrations that malloc allocated arrays bring. In particular, I've always found the fact that the size of an array must be stored separately to be a...
23
by: Ken Turkowski | last post by:
The construct (void*)(((long)ptr + 3) & ~3) worked well until now to enforce alignment of the pointer to long boundaries. However, now VC++ warns about it, undoubtedly to help things work on 64...
52
by: onsbomma | last post by:
I want to set and reset the least significant bit of a address (where a pointers points to). I tried this, but it is not correct: #define BIT 0x1 void foo(){ void *p; *p = *p & ~BIT
10
by: mdh | last post by:
Quick question about pointers. Wrote this "trying to understand this better" code: int *ptr, x = 565; ptr= &x; printf("\n\n\nThe value of x is %d\n", x);
7
by: Charles | last post by:
I'd like to develop a simple cross-platform application in C++. I'd like it to run in Windows, OS X, PC-BSD and Linux. From my research, it seems I should use Qt or Gtk as a graphical library. Do...
1
by: Carlos Martinez | last post by:
Hi all: I want to know how can I trace an address. I don't need the conversion from an integral type to string. Suppose I have an "asString" function that do that conversion. My main...
36
by: Julienne Walker | last post by:
Ignoring implementation details and strictly following the C99 standard in terms of semantics, is there anything fundamentally flawed with describing the use of a (non-inline) function as an...
4
by: gw7rib | last post by:
I'm using a system in which TCHAR is typedef-ed to represent a character, in this case a wchar_t to hold Unicode characters, and LPCTSTR is typedef-ed to be a pointer to constant wchar_t. I presume...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.