Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #7613 > unrolled thread

pointer to pointer intialize to NULL but still point to NULL

Started byChristopher <cpisz@austin.rr.com>
First post2011-07-07 15:03 -0700
Last post2011-07-09 00:35 +0000
Articles 6 — 5 participants

Back to article view | Back to comp.lang.c++


Contents

  pointer to pointer intialize to NULL but still point to NULL Christopher <cpisz@austin.rr.com> - 2011-07-07 15:03 -0700
    Re: pointer to pointer intialize to NULL but still point to NULL Paavo Helde <myfirstname@osa.pri.ee> - 2011-07-08 00:39 -0500
    Re: pointer to pointer intialize to NULL but still point to NULL Stuart Redmann <DerTopper@web.de> - 2011-07-08 00:02 -0700
      Re: pointer to pointer intialize to NULL but still point to NULL Juha Nieminen <nospam@thanks.invalid> - 2011-07-08 07:08 +0000
        Re: pointer to pointer intialize to NULL but still point to NULL Stuart Redmann <DerTopper@web.de> - 2011-07-08 00:53 -0700
    Re: pointer to pointer intialize to NULL but still point to NULL Ruben Safir <mrbrklyn@panix.com> - 2011-07-09 00:35 +0000

#7613 — pointer to pointer intialize to NULL but still point to NULL

FromChristopher <cpisz@austin.rr.com>
Date2011-07-07 15:03 -0700
Subjectpointer to pointer intialize to NULL but still point to NULL
Message-ID<8f3b364d-798b-4a27-ba68-3e2beb3a51ef@gh5g2000vbb.googlegroups.com>
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?

[toc] | [next] | [standalone]


#7627

FromPaavo Helde <myfirstname@osa.pri.ee>
Date2011-07-08 00:39 -0500
Message-ID<Xns9F1C582906704myfirstnameosapriee@216.196.109.131>
In reply to#7613
Christopher <cpisz@austin.rr.com> wrote in news:8f3b364d-798b-4a27-ba68-
3e2beb3a51ef@gh5g2000vbb.googlegroups.com:

> 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. 

Too bad as in C++ one could make use of smartpointers instead or error-
prone raw pointers (IUnknownPtr in this case).

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

ok

> 
> // Do stuff
> 
> // If failed
> return;
> 
> // If successfull
> *ppRS = pointerToInterface;

Should probably be
if (ppRS) {
    *ppRS = pointerToInterface;
    // plus some AddRef() or something	
}

> 
> Is that correct?

Depends on the details of how to handle COM pointers, I am not too 
familiar with them.

hth
Paavo

[toc] | [prev] | [next] | [standalone]


#7631

FromStuart Redmann <DerTopper@web.de>
Date2011-07-08 00:02 -0700
Message-ID<cc52b839-49fe-4a35-972c-cac7178f5a01@e7g2000vbw.googlegroups.com>
In reply to#7613
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

[toc] | [prev] | [next] | [standalone]


#7632

FromJuha Nieminen <nospam@thanks.invalid>
Date2011-07-08 07:08 +0000
Message-ID<4e16acdf$0$2838$7b1e8fa0@news.nbl.fi>
In reply to#7631
Stuart Redmann <DerTopper@web.de> 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.

[toc] | [prev] | [next] | [standalone]


#7635

FromStuart Redmann <DerTopper@web.de>
Date2011-07-08 00:53 -0700
Message-ID<b7fad030-5cce-4a05-b6b0-fb882ea0d539@eb1g2000vbb.googlegroups.com>
In reply to#7632
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

[toc] | [prev] | [next] | [standalone]


#7663

FromRuben Safir <mrbrklyn@panix.com>
Date2011-07-09 00:35 +0000
Message-ID<iv87oq$s51$1@reader1.panix.com>
In reply to#7613
Christopher <cpisz@austin.rr.com> 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...

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c++


csiph-web