473,387 Members | 1,512 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.

Casting to void*, result unexpected.

Hi,

I'm having a little bit of trouble regarding pointer casting in my program.
I don't understand why the following two cases produce different results.

Case 1:
IInterface *pInterface = new CImplementation();
pInterface->Method();

Case 2:
void* pInterface = static_cast<void*>(new CImplementation());
static_cast<IInterface*>(pInterface)->Method();

In these two cases, "IInterface" is an interface definition class, it
contains no member variables, only public pure virtual methods are defined.
"CImplementation" is derived from IInterface, and it implements those pure
virtual methods.

Now, if CImplementation is derived only from IInterface, the above two cases
will work the exact same way. However, when CImplementation derives from
other classes, which also contain virtual or pure virtual methods, Case 2
will fail. It'll cause a crash mainly because the ->Method() call isn't
actually calling IInterface::Method(), depending on the actual
inheritance, ->Method() may be pointed at a pure virtual method of another
class. Case 1 still works. Why is that? And more importantly, why is the
above two cases work differently?

Thanks,
--
He Shiming
Jul 23 '05 #1
16 2397
* He Shiming:
[unexpected result from void* casting]
...

Why is that?


Disregarding compiler-specific behavior there are only two things
you can do with a void* pointer casted from a type T: compare it wrt.
equality to another such void* pointer, and cast it back to T.

You cast to void* from a type X and then cast back to Y, and that's
Undefined Behavior even if the types X and Y are related.

Perhaps what surprises you is that static_cast can change the pointer
bitpattern (while not changing the conceptual pointer value). But if
you think about it that's how it must be. Consider:

struct A { int x; };
struct B { int y; }
struct X: A, B {};

X* pX = new X;
A* pA = static_cast<A*>( pX );
B* pB = static_cast<B*>( pX );

'pA' points to the beginning of the A subobject, and 'pB' points to
the beginning of the B subobject; these two pointers cannot be equal.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #2
> Disregarding compiler-specific behavior there are only two things
you can do with a void* pointer casted from a type T: compare it wrt.
equality to another such void* pointer, and cast it back to T.
You cast to void* from a type X and then cast back to Y, and that's
Undefined Behavior even if the types X and Y are related.
I'm sorry but what does this mean? I mentioned that it only fails when
multiple inheritance is used. Then if it's an undefined behavior, why does
it work flawlessly, when single inheritance is used?

Consider:

struct A { int x; };
struct B { int y; }
struct X: A, B {};

X* pX = new X;
A* pA = static_cast<A*>( pX );
B* pB = static_cast<B*>( pX );

'pA' points to the beginning of the A subobject, and 'pB' points to
the beginning of the B subobject; these two pointers cannot be equal.


I got your point but this isn't what exactly I wanted.

Consider case 1 in my first post, it worked with the "IInterface" type
definition on the pointer, case 2 fails because something bad happened
during pointer re-casting. The reason why I'm trying to use void* is because
I can't store the type definition in this particular case. In the first
line, the class allocator has no idea about what type should it use.

Regarding the pointer itself. Isn't the pointer just an address? Isn't void*
capable of storing any kind of pointers? If so, why won't casting work?

Thanks,
He Shiming
Jul 23 '05 #3
* "He Shiming" <mailbill(NOSPAM)@21cn.com.nospam>:
Disregarding compiler-specific behavior there are only two things
you can do with a void* pointer casted from a type T: compare it wrt.
equality to another such void* pointer, and cast it back to T.
You cast to void* from a type X and then cast back to Y, and that's
Undefined Behavior even if the types X and Y are related.
I'm sorry but what does this mean? I mentioned that it only fails when
multiple inheritance is used. Then if it's an undefined behavior, why does
it work flawlessly, when single inheritance is used?


Undefined Behavior means the effect can be anything, including whatever you
think it "should" be.

Don't rely on Undefined Behavior.

Consider:

struct A { int x; };
struct B { int y; }
struct X: A, B {};

X* pX = new X;
A* pA = static_cast<A*>( pX );
B* pB = static_cast<B*>( pX );

'pA' points to the beginning of the A subobject, and 'pB' points to
the beginning of the B subobject; these two pointers cannot be equal.


I got your point but this isn't what exactly I wanted.

Consider case 1 in my first post, it worked with the "IInterface" type
definition on the pointer,


Case 1:
IInterface *pInterface = new CImplementation();
pInterface->Method();

case 2
Case 2:
void* pInterface = static_cast<void*>(new CImplementation());
static_cast<IInterface*>(pInterface)->Method();

fails because something bad happened during pointer re-casting.
Nope, it fails because it can. It _can_ fail because it's UB. However,
if the type you cast from is the as same the one you cast to it's no longer
UB and won't fail:

void* pInterface = static_cast<IInterface*>( new CImplementation() );
static_cast<IInterface*>( pInterface )->Method();

Not that I recommend such things.

Don't use void*.

The reason why I'm trying to use void* is because
I can't store the type definition in this particular case.
If you can cast the pointer back from void*, then you know the type and
don't have to use void* in the first place.

Simple.

In the first
line, the class allocator has no idea about what type should it use.
?

Regarding the pointer itself. Isn't the pointer just an address?
In general no, but for most pointers, yes.

Isn't void* capable of storing any kind of pointers?
No, but it's capable of storing the kind you're using here.

If so, why won't casting work?


Because you're casting from a type X and back to a different type Y.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #4
I'm not sure if you are aware of this. But in Microsoft COM APIs, there is
this CoCreateInstance function that does use void* pointer casting. The
function definition went like this:

STDAPI CoCreateInstance(
REFCLSID rclsid,
LPUNKNOWN pUnkOuter,
DWORD dwClsContext,
REFIID riid,
LPVOID * ppv
);

When we use this function, we write something like this:
CComPtr<Interface> spInterface;
CoCreateInstance(blahblahblah...., (void**) &spInteface);

CComPtr is a smart pointer, there is no void* operators defined in it. And
as you can see here, we do cast such pointers. It's pretty much what I'm
trying to do. You can see it initiates the class object to void*, and in the
smart pointer, in order to use it, it'll re-cast it back. If
CoCreateInstance produces a void* pointer, as you said should have undefined
behavior, and may or may not work. How are we going to use COM interfaces
correctly?

Sorry for being Microsoft-specific, but it certainly explains that it should
work.

--
He Shiming

"Alf P. Steinbach" <al***@start.no> wrote in message
news:42****************@news.individual.net...
Not that I recommend such things.

Don't use void*.
If you can cast the pointer back from void*, then you know the type and
don't have to use void* in the first place.

Simple.

In the first
line, the class allocator has no idea about what type should it use.


?

Regarding the pointer itself. Isn't the pointer just an address?


In general no, but for most pointers, yes.

Isn't void* capable of storing any kind of pointers?


No, but it's capable of storing the kind you're using here.

If so, why won't casting work?


Because you're casting from a type X and back to a different type Y.

Jul 23 '05 #5
* He Shiming:
[top-posting]
Please don't top-post in this group. See the FAQ. Corrected.
* He Shiming: * Alf P. Steinbach:
* He Shiming:
..., why won't casting work?
Because you're casting from a type X and back to a different type Y.


I'm not sure if you are aware of this. But in Microsoft COM APIs, there is
this CoCreateInstance function that does use void* pointer casting. The
function definition went like this:

STDAPI CoCreateInstance(
REFCLSID rclsid,
LPUNKNOWN pUnkOuter,
DWORD dwClsContext,
REFIID riid,
LPVOID * ppv
);


So? Ask for a pointer to the correct interface type. When the pointer
originates from type IWhatever* you can safely cast it to IWhatever*
(if you're unsure about how to specify types vis-a-vis the Microsoft
COM API, please ask about that in a Microsoft-specific group, not here).

But better, don't use void* pointers yourself.

Have I mentioned that?

...
Sorry for being Microsoft-specific, but it certainly explains that it should
work.


The example bears no relation to your "non-working" example. In that example
you casted from a type X to void*, and then back to a different type Y.
That's not the case in the example you're now giving.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #6
>* He Shiming:
[top-posting]
Please don't top-post in this group. See the FAQ. Corrected.


I don't even know how to "top-post". And how can you correct that? Just
because your Forte hates my Outlook Express doesn't justify my top-posting.


So? Ask for a pointer to the correct interface type. When the pointer
originates from type IWhatever* you can safely cast it to IWhatever*
(if you're unsure about how to specify types vis-a-vis the Microsoft
COM API, please ask about that in a Microsoft-specific group, not here).


I said sorry for being Microsoft-specific already. But if you have no idea
about how CoCreateInstance works, you have no idea about what I'm doing
either. FYI, CoCreateInstance works pretty much (although not exactly) the
same way as my Case 2 example.

"When the pointer originates from type IWhatever* you can safely cast it to
IWhatever*" <-- Well, how did you come up with that? Previously, you said
that as type B is different, one can't cast from type A to B, and then cast
from B back to A. And now you are saying that we can?

Well, given the fact the Case 2 doesn't work as expected, and I do need to
cast the specialized pointer to something general, then later cast it back.
How would you recommend a solution, or your are saying that there's no
solution at all?

Thanks,
He Shiming
Jul 23 '05 #7
You know, I got it worked.

It's as simple as this:

void* pInterface = static_cast<void*>(static_cast<IInterface*>(new
CImplementation());

That's right, casting twice. And it also proved my point, that void* should
work, the pointer is only an address.

--
He Shiming
Jul 23 '05 #8
* "He Shiming" <mailbill(NOSPAM)@21cn.com.nospam>:
* He Shiming:
[top-posting]
Please don't top-post in this group. See the FAQ. Corrected.


I don't even know how to "top-post". And how can you correct that? Just
because your Forte hates my Outlook Express doesn't justify my top-posting.


See the FAQ.

So? Ask for a pointer to the correct interface type. When the pointer
originates from type IWhatever* you can safely cast it to IWhatever*
(if you're unsure about how to specify types vis-a-vis the Microsoft
COM API, please ask about that in a Microsoft-specific group, not here).


I said sorry for being Microsoft-specific already. But if you have no idea
about how CoCreateInstance works, you have no idea about what I'm doing
either. FYI, CoCreateInstance works pretty much (although not exactly) the
same way as my Case 2 example.


Case 2:
void* pInterface = static_cast<void*>(new CImplementation());
static_cast<IInterface*>(pInterface)->Method();

That's _very_ different from the OS API function: this case 2 code casts from
a type X = CImplementation* to void*, and then from void* to a different type
Y = IInterface*.

Is that unclear somehow?

Or do you believe that the OS API mentioned above does exactly that?

It doesn't: it gives you a pointer casted from the type you specify, to void*,
so simply specify the type you'll cast it back to.

"When the pointer originates from type IWhatever* you can safely cast it to
IWhatever*" <-- Well, how did you come up with that? Previously, you said
that as type B is different, one can't cast from type A to B, and then cast
from B back to A.
No, I didn't write that: I told you you get UB if you cast from A* to void*
and from that void* to B*, when A and B are different types.

And now you are saying that we can?
No, not that either.

I suggest you read the earlier postings again, with attention to detail.

Well, given the fact the Case 2 doesn't work as expected, and I do need to
cast the specialized pointer to something general, then later cast it back.
How would you recommend a solution, or your are saying that there's no
solution at all?


See above.

And the earlier replies in this thread.

The implication of those replies: figure out how to use that smart pointer
class you're using so as to avoid having void* pointers around... ;-)

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #9
Okay my bad.

--
He Shiming
"Alf P. Steinbach" <al***@start.no> wrote in message
news:42****************@news.individual.net...
* "He Shiming" <mailbill(NOSPAM)@21cn.com.nospam>:
>* He Shiming:
>> [top-posting]
>
> Please don't top-post in this group. See the FAQ. Corrected.


I don't even know how to "top-post". And how can you correct that? Just
because your Forte hates my Outlook Express doesn't justify my
top-posting.


See the FAQ.

> So? Ask for a pointer to the correct interface type. When the pointer
> originates from type IWhatever* you can safely cast it to IWhatever*
> (if you're unsure about how to specify types vis-a-vis the Microsoft
> COM API, please ask about that in a Microsoft-specific group, not
> here).
>


I said sorry for being Microsoft-specific already. But if you have no
idea
about how CoCreateInstance works, you have no idea about what I'm doing
either. FYI, CoCreateInstance works pretty much (although not exactly)
the
same way as my Case 2 example.


Case 2:
void* pInterface = static_cast<void*>(new CImplementation());
static_cast<IInterface*>(pInterface)->Method();

That's _very_ different from the OS API function: this case 2 code casts
from
a type X = CImplementation* to void*, and then from void* to a different
type
Y = IInterface*.

Is that unclear somehow?

Or do you believe that the OS API mentioned above does exactly that?

It doesn't: it gives you a pointer casted from the type you specify, to
void*,
so simply specify the type you'll cast it back to.

"When the pointer originates from type IWhatever* you can safely cast it
to
IWhatever*" <-- Well, how did you come up with that? Previously, you said
that as type B is different, one can't cast from type A to B, and then
cast
from B back to A.


No, I didn't write that: I told you you get UB if you cast from A* to
void*
and from that void* to B*, when A and B are different types.

And now you are saying that we can?


No, not that either.

I suggest you read the earlier postings again, with attention to detail.

Well, given the fact the Case 2 doesn't work as expected, and I do need
to
cast the specialized pointer to something general, then later cast it
back.
How would you recommend a solution, or your are saying that there's no
solution at all?


See above.

And the earlier replies in this thread.

The implication of those replies: figure out how to use that smart pointer
class you're using so as to avoid having void* pointers around... ;-)

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

Jul 23 '05 #10
* "He Shiming" <mailbill(NOSPAM)@21cn.com.nospam>:
You know, I got it worked.

It's as simple as this:

void* pInterface = static_cast<void*>(static_cast<IInterface*>(new
CImplementation());
That's good; except for the unnecessary cast (figure it out ;-)) that's
the code I gave you in my second posting.

That's right, casting twice. And it also proved my point, that void* should
work, the pointer is only an address.


Unfortunately, that's not-so-good: it seems you're misunderstanding why
it works.

Here's why it works: you're now casting from a type T to void*, and back
to the same type T (where T = IInterface*).

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #11
* He Shiming:
[top-posting]
Please don't top-post in this group. See the FAQ.

Okay my bad.


Heh... ;-)

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #12
ben

"He Shiming" <mailbill(NOSPAM)@21cn.com.nospam> wrote in message
news:d9**********@news.yaako.com...
I'm not sure if you are aware of this. But in Microsoft COM APIs, there is
this CoCreateInstance function that does use void* pointer casting. The
function definition went like this:

STDAPI CoCreateInstance(
REFCLSID rclsid,
LPUNKNOWN pUnkOuter,
DWORD dwClsContext,
REFIID riid,
LPVOID * ppv
);


This relies on the fact that ALL COM interface must inherit from the
IUnknown. So no matter which interface is pointed to by whatever returned
from CoCreateInstance, it starts with IUnknow memory layout. It is from
IUnknown::QueryInterface that you start to do more specific interface casts.
That is, the three methods in IUnknown is guaranteed to work correctly;
calls to other methods are still undefined.

ben
Jul 23 '05 #13
ben
If you are maintaining a single inheritance tree, the code pretty much works
because all subobjects begins with the same memory address in most cases.

But if you start to get into multiple inheritance things are a little
tricky, because subobjects no longer can share the same beginning address,
so the cast operator must do work to correctly offset the addresses from
type to type. This can only be done with some type information available to
the compiler. The void* doesn't have any type information for the compiler
to correctly offset the memory addresses.

Stan Lipman's "Inside the C++ Object Model" has an in depth explanation
throughout the book.

ben
Jul 23 '05 #14
ben wrote:
This relies on the fact that ALL COM interface must inherit from the
IUnknown. So no matter which interface is pointed to by whatever returned
from CoCreateInstance, it starts with IUnknow memory layout. It is from
IUnknown::QueryInterface that you start to do more specific interface casts.
That is, the three methods in IUnknown is guaranteed to work correctly;
calls to other methods are still undefined.

This is really no different than the preivous example where he made a
cast to the base class before stuffing it in void. Frankly, if
Microsoft had a clue these things would use IUnknown* rather than
void*.

-Ron

IUnkonwn is the all-day sucker of COM interfaces.
Jul 23 '05 #15
He Shiming wrote:
* He Shiming:
[top-posting]
Please don't top-post in this group. See the FAQ. Corrected.


I don't even know how to "top-post".


Funny, considering that you're doing it all the time (except in this
posting) :-)
And how can you correct that?
See the FAQ.
Just because your Forte hates my Outlook Express doesn't justify my
top-posting.


Yup, right. Whatever other people's newsreaders think about yours, you are
not supposed to be top-posting, so don't do it.

Jul 23 '05 #16
ben
This is really no different than the preivous example where he made a
cast to the base class before stuffing it in void. Frankly, if
Microsoft had a clue these things would use IUnknown* rather than
void*.
Using IUnknown* to return a generic interface pointer is sweet, but because
QueryInterface mandates that every call that requests the same interface
must return the same address, we might get one IUnknown* from
CoCreateInterace and get another IUnknow* from QueryInterface and we might
accidentally assume that these are two distinct objects because they have
two different IUnknown*....

So Microsoft has a reason of preferring void** over IUnknown**

ben

-Ron

IUnkonwn is the all-day sucker of COM interfaces.

Jul 23 '05 #17

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

Similar topics

4
by: Jacob Jensen | last post by:
This question has probably been asked a million time, but here it comes again. I want to learn the difference between the three type cast operators: static_cast, reinterpret_cast, dynamic_cast. A...
231
by: Brian Blais | last post by:
Hello, I saw on a couple of recent posts people saying that casting the return value of malloc is bad, like: d=(double *) malloc(50*sizeof(double)); why is this bad? I had always thought...
33
by: hermit_crab67 | last post by:
Can someone explain to a C newbie why this doesn't work as I expect it to work? (expectations clearly outlined in the printf statement in main routine) OS: Linux 2.4.26 GCC: 2.95.4 void...
35
by: ytrama | last post by:
Hi, I have read in one of old posting that don't cast of pointer which is returned by the malloc. I would like to know the reason. Thanks in advance, YTR
18
by: Marco | last post by:
I need to get a iterator from any generic collection. public class .... GetIterator(Object collection) { ..... }
12
by: 6tc1 | last post by:
Hi all, I just discovered a rounding error that occurs in C#. I'm sure this is an old issue, but it is new to me and resulted in a fair amount of time trying to track down the issue. Basically...
33
by: Mark P | last post by:
A colleague asked me something along the lines of the following today. For some type X he has: X* px = new X; Then he wants to convert px to a char* (I'm guessing for the purpose of...
3
by: Beta What | last post by:
Hello, I have a question about casting a function pointer. Say I want to make a generic module (say some ADT implementation) that requires a function pointer from the 'actual/other modules'...
8
by: Neha | last post by:
Hi all Its seems bit silly que but pleasee explan me why this error is coming ? and what will be the solution if i want void * to be intilaize by struct * and this code is puerly in C. --...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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.