By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,778 Members | 1,957 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,778 IT Pros & Developers. It's quick & easy.

will the pointer survive?

P: n/a
Hi all,

Will my pointer survive this conversion?

MyType_tag MyType;
MyType_tag* pMyPntr = &MyType;
int iUglyTempStorage = (int)pMyPntr;
MyType_tag* pMyOtherPntr = (MyType_tag*)iUglyTempStorage;
--

Best wishes,
Allen


Jul 19 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
"Allen" <allen-terri-ng!@#att.net> wrote in message
news:ay*********************@bgtnsc05-news.ops.worldnet.att.net...
Will my pointer survive this conversion?

MyType_tag MyType;
MyType_tag* pMyPntr = &MyType;
int iUglyTempStorage = (int)pMyPntr;
MyType_tag* pMyOtherPntr = (MyType_tag*)iUglyTempStorage;


No. (except on some platforms)
hth,
Ivan
--
http://ivan.vecerina.com
Jul 19 '05 #2

P: n/a

"Allen" <allen-terri-ng!@#att.net> wrote in message
news:ay*********************@bgtnsc05-news.ops.worldnet.att.net...
Hi all,

Will my pointer survive this conversion?

MyType_tag MyType;
MyType_tag* pMyPntr = &MyType;
int iUglyTempStorage = (int)pMyPntr;
MyType_tag* pMyOtherPntr = (MyType_tag*)iUglyTempStorage;
--

Best wishes,
Allen


Not necessarily, though your conversion will (more or less) succeed as
you're forcing the compiler to obey your wish. Generally you might get away
with this on some platforms but it's not guaranteed to work and it's
certainly not portable.

Chris
Jul 19 '05 #3

P: n/a
Allen wrote:
Hi all,

Will my pointer survive this conversion?

MyType_tag MyType;
MyType_tag* pMyPntr = &MyType;
int iUglyTempStorage = (int)pMyPntr;
MyType_tag* pMyOtherPntr = (MyType_tag*)iUglyTempStorage;


Only if you're using a platform where an int is the same size as a pointer.

--
Unforgiven

"You can't rightfully be a scientist if you mind people thinking
you're a fool."
Jul 19 '05 #4

P: n/a
> int iUglyTempStorage = (int)pMyPntr;

The above line is the source of trouble. Here it assumes that the
pointer and the integer are of the same size. This may not be
true on several platforms.

Sandeep
--
http://www.EventHelix.com/EventStudio
EventStudio 2.0 - Generate Sequence Diagrams and Use Case Diagrams in PDF
Jul 19 '05 #5

P: n/a
On Fri, 14 Nov 2003 05:15:50 +0000, Allen wrote:
Will my pointer survive this conversion?

MyType_tag MyType;
MyType_tag* pMyPntr = &MyType;
int iUglyTempStorage = (int)pMyPntr;
MyType_tag* pMyOtherPntr = (MyType_tag*)iUglyTempStorage;


Will this work?
MyType_tag MyType;
MyType_tag* pMyPntr = &MyType;
char *ugly[sizof(MyType_tag*)];
memcpy(ugly,&pMyPtr,sizeof(MyType_tag*));
MyType_tag* pMyOtherPntr;
memcpy(&pMyOtherPntr,ugly,sizeof(MyType_tag*));
That made me feel really bad :( I think I have to go take a shower and
wash my mouth out with soap ;)

--
NPV

"the large print giveth, and the small print taketh away"
Tom Waits - Step right up

Jul 19 '05 #6

P: n/a
"EventHelix.com" <ev********@hotmail.com> wrote in message
news:56**************************@posting.google.c om...
int iUglyTempStorage = (int)pMyPntr;


The above line is the source of trouble. Here it assumes that the
pointer and the integer are of the same size. This may not be
true on several platforms.


But this should only be a compile-time issue, right? Once it's
compiled, the binary image should no longer be dependant on the sizes of
types on various platforms should it?
Jul 19 '05 #7

P: n/a
On Fri, 14 Nov 2003 21:22:06 GMT, "Allen" <allen-terri-ng!@#att.net>
wrote in comp.lang.c++:
"EventHelix.com" <ev********@hotmail.com> wrote in message
news:56**************************@posting.google.c om...
int iUglyTempStorage = (int)pMyPntr;


The above line is the source of trouble. Here it assumes that the
pointer and the integer are of the same size. This may not be
true on several platforms.


But this should only be a compile-time issue, right? Once it's
compiled, the binary image should no longer be dependant on the sizes of
types on various platforms should it?


Once it's compiled, the binary image will not be compatible with
various platforms that have different sizes for types.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Jul 19 '05 #8

P: n/a
Allen wrote:
"EventHelix.com" <ev********@hotmail.com> wrote in message
news:56**************************@posting.google.c om...
> int iUglyTempStorage = (int)pMyPntr;


The above line is the source of trouble. Here it assumes that the
pointer and the integer are of the same size. This may not be
true on several platforms.


But this should only be a compile-time issue, right? Once it's
compiled, the binary image should no longer be dependant on the sizes
of types on various platforms should it?


Well, I don't know what you mean, but if you compile it for a platform
on which a pointer is bigger than an int (like e.g. virtually all 64bit
platforms), it will never work, even if there are other platforms on
which int and pointers are the same size.

Jul 19 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.