473,387 Members | 1,492 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,387 software developers and data experts.

will the pointer survive?

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
8 1990
"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

"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
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
> 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
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
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: john townsley | last post by:
when using vectors with pointers of objects. is it best to create instances then point to them or dynamic allocation upon creating a vector element. I can do this, but is creating a vector with...
3
by: Welman Jordan | last post by:
Hello all, In Session_OnStart, I set a session variable: Session = DateTime.Now; and Session.Timeout = 15;
9
by: Pravin A. Sable | last post by:
Dear All, I am VB programmer, from VB 3.0... to VB.6.0. I love VB :-). I wish to move to VB.net, but I have been advised by certain people to move to C#. They even claim that VB.net will die down...
5
by: Johs32 | last post by:
I have a struct "my_struct" and a function that as argument takes a pointer to this struct: struct my_struct{ struct my_struct *new; }; void my_func(struct my_struct *new); I have read...
8
by: Axter | last post by:
I normally use a program call Doxygen to document my source code.(http://www.stack.nl/~dimitri/doxygen) This method works great for small and medium size projects, and you can get good...
37
by: jasmith | last post by:
How will Access fair in a year? Two years? .... The new version of Access seems to service non programmers as a wizard interface to quickly create databases via a fancy wizard. Furthermore, why...
32
by: seank76 | last post by:
While going through my company's existing codebase, I saw a bunch of weird lines. Take a look at this one: class A { public: A(Foo *f) : _f(f) {}
11
by: Army1987 | last post by:
Is this code legal (according to the strictest possible sane interpretation of the standard, regardless of wheter it does work on all implementations in the known universe)? #include <stdio.h>...
50
by: arunajob | last post by:
Hi all, If I have a piece of code something like this void main(void) { char * p1="abcdefghijklmn"; ............................................. }
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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:
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...

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.