Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > pointer to pointer intialize to NULL but still point to NULL

Reply
Thread Tools

pointer to pointer intialize to NULL but still point to NULL

 
 
Christopher
Guest
Posts: n/a
 
      07-07-2011
void Foo(IUnknown ** ppMicrosoftsOut)
{
// If the operation fails param is NULL

// Otherwise param is a valid pointer to object
}


I can't change the function prototype, because there is already a lot
of code that depends on it. How do I initialize the parameter properly
in the function body, such that all of the following calls from the
client yield the expected results?

1) Foo(NULL);
2) IUnknown * result = somePointerToAlreadyInstantiatedObject;
Foo(&result);
3) IUnknown * result = NULL;
Foo(&result);

This is my attempt:

if( ppRS && *ppRS )
{
(*ppRS)->Release();
(*ppRS) = NULL;
}

// Do stuff

// If failed
return;

// If successfull
*ppRS = pointerToInterface;

Is that correct?
 
Reply With Quote
 
 
 
 
Stuart Redmann
Guest
Posts: n/a
 
      07-08-2011
On 8 Jul., Christopher wrote:
> void Foo(IUnknown ** ppMicrosoftsOut)
> {
> // If the operation fails param is NULL
>
> // Otherwise param is a valid pointer to object
>
> }
>
> I can't change the function prototype, because there is already a lot
> of code that depends on it. How do I initialize the parameter properly
> in the function body, such that all of the following calls from the
> client yield the expected results?


Note that this is a MS COM specific question that is off-topic here.
You should try microsoft.public.vc.mfc, that is the only group that
shows at least some activity (the dedicated COM groups were never very
active, even back in the time when MS had not declared its groups
dead) or use the social.msdn site (they dropped their newsgroup in
favour of this credit-driven web-based pseudo-newsgroup).

> 1) Foo(NULL);


That is a case that you should not be bothered with because the rules
of COM actually forbid that an [out]-parameter is initialized with a
NULL pointer. The correct answer to such a case would be
HRESULT Foo (IUnknown ** ppMicrosoftsOut)
{
if (!ppMicrosoftsOut)
return E_POINTER;

// whatever.
return S_OK;
}

Note that you would have to make the method more COM-compliant by
changing the return type into an HRESULT.

> 2) IUnknown * result = somePointerToAlreadyInstantiatedObject;
> Foo(&result);
> 3) IUnknown * result = NULL;
> Foo(&result);
>


This depends on whether the parameter ppMicrosoftsOut is declared as
[in, out] or just as [out] parameter. Pointer parameters that are
[out] and not also [in] should be initialized with a NULL pointer be
the caller, so you could write:

HRESULT Foo (IUnknown ** ppMicrosoftsOut)
{
if (!ppMicrosoftsOut)
return E_POINTER;

ASSERT (!*ppMicrosoftsOut);

// whatever.
return S_OK;
}


> This is my attempt:
>
> if( ppRS && *ppRS )
> {
> (*ppRS)->Release();
> (*ppRS) = NULL;
>
> }
>
> // Do stuff
>
> // If failed
> return;
>
> // If successfull
> *ppRS = pointerToInterface;
>
> Is that correct?


Yes, that should always work (for both the [out] and the [in, out]
case).


I strongly recommend that you use the automatically generated wrappers
that you get when you #import type libraries (you'll get those .tlh
and .tli files). They use a smart pointer _com_ptr_t that relieve you
from having to manage the refcount of COM pointers.

Regards,
Stuart
 
Reply With Quote
 
 
 
 
Juha Nieminen
Guest
Posts: n/a
 
      07-08-2011
Stuart Redmann <(E-Mail Removed)> wrote:
> Note that this is a MS COM specific question that is off-topic here.


How exactly? I didn't see anything in the posted code that couldn't be
just regular standard C++ (when the used names are defined properly).

Just because a variable name happens to contain the word "Microsoft" in
it doesn't mean the posted code is "MS COM specific". He could have changed
it to something else and you would be none the wiser.
 
Reply With Quote
 
Stuart Redmann
Guest
Posts: n/a
 
      07-08-2011
Stuart Redmann wrote:
> > Note that this is a MS COM specific question that is off-topic here.


On 8 Jul., Juha Nieminen wrote:
> * How exactly? I didn't see anything in the posted code that couldn't be
> just regular standard C++ (when the used names are defined properly).


Really? I thought that at least _you_ would recognize a COM specific
question if you saw one.

> * Just because a variable name happens to contain the word "Microsoft" in
> it doesn't mean the posted code is "MS COM specific". He could have changed
> it to something else and you would be none the wiser.


Then I'd have said that I would need to know the definition of
IUnknown, and why he had called Release on it. That's how _I_ spotted
that it is a COM specific question.

If I had told him that from a C++ point of view it is totally OK to
pass a NULL pointer into Foo, he would not not have gained much
information (I think he already knows that).

I know that I should have refrained from answering when I already had
spotted that this question is kind of off-topic here. I didn't because
I am probably the last Knight of COM (all the other members of Round
Table have joined the Kingdom of DotNet), so I'm happy to see some new
followers.

SCNR,
Stuart

 
Reply With Quote
 
Ruben Safir
Guest
Posts: n/a
 
      07-09-2011
Christopher <(E-Mail Removed)> wrote:
> void Foo(IUnknown ** ppMicrosoftsOut)
> {
> // If the operation fails param is NULL
>
> // Otherwise param is a valid pointer to object
> }
>
>
> I can't change the function prototype, because there is already a lot
> of code that depends on it. How do I initialize the parameter properly
> in the function body, such that all of the following calls from the
> client yield the expected results?
>


My expereince is that C++ despises null...

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Share-Point-2010 ,Share-Point -2010 Training , Share-point-2010Hyderabad , Share-point-2010 Institute Saraswati lakki ASP .Net 0 01-06-2012 06:39 AM
Why is the pointer passed into the function still NULL? Malcolm Nooning C Programming 8 04-21-2011 01:37 PM
Null pointer (NULL array pointer is passed) aneuryzma C++ 3 06-16-2008 05:48 AM
Scenario 5: IS-IS routing on Frame Relay Multi-point and Point-to-Point David Sudjiman Cisco 0 06-08-2006 09:11 AM
"stringObj == null" vs "stringObj.equals(null)", for null check?? qazmlp1209@rediffmail.com Java 5 03-29-2006 10:37 PM



Advertisments