|
From: David Wilkinson on 6 May 2008 12:30 Tom Serface wrote: > I don't think you can cast the pointer that way. You could simplify > this by doing something like: > > void MyFunc(_int64 *a) > { > _int64 z = *a; > } > > > long x; > x = 5; > _int64 y = x; // This conversion done OK. > MyFunc(&y); Tom: Actually, it seems you can cast the pointer. Try this #include <iostream> void someFunc(__int64 *a) { (*a) *= 2; } int main() { long x = 2; someFunc( (__int64 *) &x) ; std::cout << "x = " << x << std::endl; } It gives the answer 4 (on VC/Windows). But I agree that it seems it should not work. It must be that the layout of __int64 places the lower 32 bits at the beginning. To the OP: it's not a good idea to cast pointers, though it can work sometimes on a specific machine. -- David Wilkinson Visual C++ MVP
From: Doug Harrison [MVP] on 6 May 2008 12:48 On Tue, 6 May 2008 08:28:49 -0700 (PDT), "rogFed28685(a)gmail.com" <rogFed28685(a)gmail.com> wrote: >Hi all, >I was wondering if we could pass in an long value in an __int64 >pointer variable and vice versa. > >So something like, > >someFunc(__ int64 *a) >{ >do something; >} > >int main() >{ > long x; > someFunc( ( __int64 *) &a) ; >} > >Is it possible to send a long addreess value through an __int64 >pointer variable? If not, how does one go about it? All data pointers have the same size, and as long as someFunc casts "a" back to long* before dereferencing it, it'll work. Of course, it would be disastrous to pretend "a" holds a pointer to an __int64 (8 byte integer) when it really holds a pointer to a long (4 byte integer). What are you trying to accomplish? >Any input is really appreciated...Im a newbie to pointers so if Q >seems silly I apologize. Using the new-style casts will help you avoid mistakes. Above, you need to use reinterpret_cast. -- Doug Harrison Visual C++ MVP
From: Doug Harrison [MVP] on 6 May 2008 12:50 On Tue, 06 May 2008 12:30:16 -0400, David Wilkinson <no-reply(a)effisols.com> wrote: >Tom: > >Actually, it seems you can cast the pointer. Try this > >#include <iostream> > >void someFunc(__int64 *a) >{ > (*a) *= 2; >} > >int main() >{ > long x = 2; > someFunc( (__int64 *) &x) ; > std::cout << "x = " << x << std::endl; >} > >It gives the answer 4 (on VC/Windows). But I agree that it seems it should not >work. It must be that the layout of __int64 places the lower 32 bits at the >beginning. It causes a buffer overrun. -- Doug Harrison Visual C++ MVP
From: Joseph M. Newcomer on 6 May 2008 23:52 Since you are a "pointer newbie", let me first say that what you are trying to do sounds fundamentally wrong; you would need a good excuse for doing this. You have provided no rationale as to why this would make sense. There are times when you need to pass a pointer via an integer variable, in which case, it is rare to the point of nonexistent that you would use __int64 *. You would use UINT_PTR, INT_PTR, DWORD_PTR, WPARAM, LPARAM, or LRESULT as the data type. But what you are doing below is essentially nonsense. You have a pointer to a long (I presume you meant to write &x as the parameter, not &a), and you are passing it as a pointer to some other data type of a different size. This doesn't make much sense. If you have a long, you would pass it as a long. If you have an __int64, you would pass a pointer to it as an __int64. But passing a pointer to an object of one size by declaring it as a pointer to an object of another size, which is larger, is fundamentally Bad Practice. So why do you want to do bad practice? Note that you would very rarely, if ever, use __int64 as a data type. You might have LONGLONG or ULONGLONG, but writing the base type is not common practice. What do you think you are attempting to do? And why do you think it makes sense for you to do this? joe On Tue, 6 May 2008 08:28:49 -0700 (PDT), "rogFed28685(a)gmail.com" <rogFed28685(a)gmail.com> wrote: >Hi all, >I was wondering if we could pass in an long value in an __int64 >pointer variable and vice versa. > >So something like, > >someFunc(__ int64 *a) >{ >do something; >} > >int main() >{ > long x; > someFunc( ( __int64 *) &a) ; >} > >Is it possible to send a long addreess value through an __int64 >pointer variable? If not, how does one go about it? > >Any input is really appreciated...Im a newbie to pointers so if Q >seems silly I apologize. Joseph M. Newcomer [MVP] email: newcomer(a)flounder.com Web: http://www.flounder.com MVP Tips: http://www.flounder.com/mvp_tips.htm
From: David Wilkinson on 7 May 2008 12:42 Doug Harrison [MVP] wrote: > On Tue, 06 May 2008 12:30:16 -0400, David Wilkinson <no-reply(a)effisols.com> > wrote: > >> Tom: >> >> Actually, it seems you can cast the pointer. Try this >> >> #include <iostream> >> >> void someFunc(__int64 *a) >> { >> (*a) *= 2; >> } >> >> int main() >> { >> long x = 2; >> someFunc( (__int64 *) &x) ; >> std::cout << "x = " << x << std::endl; >> } >> >> It gives the answer 4 (on VC/Windows). But I agree that it seems it should not >> work. It must be that the layout of __int64 places the lower 32 bits at the >> beginning. > > It causes a buffer overrun. Doug: You are right of course. -- David Wilkinson Visual C++ MVP
First
|
Prev
|
Pages: 1 2 Prev: Running Process in the Background Next: list of vc6 preprocessor switches? |