will the pointer survive?

A

Allen

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

Ivan Vecerina

Allen said:
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
 
C

Chris Theis

Allen said:
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;

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
 
U

Unforgiven

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

EventHelix.com

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
 
N

Nils Petter Vaskinn

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 ;)
 
A

Allen

EventHelix.com said:
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?
 
J

Jack Klein

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
 
R

Rolf Magnus

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

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

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
EmeliaBryc

Latest Threads

Top