Topic: NULLing a pointer


Author: markr@csc.liv.ac.uk (Mark Rivers)
Date: 14 Oct 92 00:30:30 GMT
Raw View
Crunchy Frog writes:

> Kayvon Z. Sadeghi writes:
>
> >1- how do you change the value of a pointer passed to a subroutine? It seems
> >to me like you can do almost anything with it except changing the value
> >to NULL. This is what I mean:
>
> Let me ask you a question: I have a frobznick.  How do I change the value
> of the frobznick in a subroutine?  I pass a pointer to it.  How, then, do
> I change the value of a pointer?  I pass a pointer to that pointer.
>
> [code rearranged]
>
> changeMe(int **thing) { *thing = NULL; }
> main()
> {
>   int *fred;
>   changeMe(&fred);
> }

A more elegant solution in C++ is to use a reference to a pointer.

void changeMe(int*& thing) { thing = NULL; }
main()
{
    int *fred;
    changeMe(fred);
}

This looks exactly like your original code except that the changeMe
declaration specifies call-by-reference on "thing".

 Mark

____________________________________________________________________________
Mark Rivers,                                  |
Department of Computer Science,               |
Chadwick Building, University of Liverpool,   |   Voice: +44 51 794 3684
P.O. Box 147, Brownlow Hill,                  |   Fax:   +44 51 794 3715
Liverpool L69 3BX, UNITED KINGDOM.            |   Email: markr@csc.liv.ac.uk




Author: warsaw@nlm.nih.gov (Barry A. Warsaw)
Date: 15 Oct 92 15:34:15 GMT
Raw View
On a related topic, I've always wondered why operator delete was
required to take a void* as its first arg and not void*&.  I
understand why a deleted ptr is not nulled by default (and agree with
the reasoning), but I should be able to add that functionality to my
classes if I'm willing to incur any associated costs.  With
delete(void*), I can't even do it.




Author: warsaw@nlm.nih.gov (Barry A. Warsaw)
Date: 15 Oct 92 18:28:42 GMT
Raw View
    me> With delete(void*), I can't even do it.

I should have said, I can't do it with op delete. I have to use some
other method name.  No big deal, I s'pose...




Author: frank@Cookie.secapl.com (Frank Adams)
Date: Thu, 15 Oct 1992 20:31:19 GMT
Raw View
In article <WARSAW.92Oct15103415@anthem.nlm.nih.gov> warsaw@nlm.nih.gov (Barry A. Warsaw) writes:
>On a related topic, I've always wondered why operator delete was
>required to take a void* as its first arg and not void*&.  I
>understand why a deleted ptr is not nulled by default (and agree with
>the reasoning), but I should be able to add that functionality to my
>classes if I'm willing to incur any associated costs.  With
>delete(void*), I can't even do it.

Because there is no guarantee that the pointer being deleted has the same
bit format as a void *.  If you try,

class Foo {...};
Foo *p;
void *& rP = p;

you should get an error message from your compiler; the types are not the
same.  But this is exactly the assignment implicit in calling operator
delete(void *&);




Author: maxtal@extro.ucc.su.OZ.AU (John MAX Skaller)
Date: Fri, 16 Oct 1992 13:20:18 GMT
Raw View
In article <WARSAW.92Oct15103415@anthem.nlm.nih.gov> warsaw@nlm.nih.gov (Barry A. Warsaw) writes:
>
>On a related topic, I've always wondered why operator delete was
>required to take a void* as its first arg and not void*&.  I
>understand why a deleted ptr is not nulled by default (and agree with
>the reasoning),

 Why is a nonconst pointer not nulled? Is it to allow comparisons
with other pointers that *ought* to be nulled?

>but I should be able to add that functionality to my
>classes if I'm willing to incur any associated costs.  With
>delete(void*), I can't even do it.

 delete p;
 p=0;

is a real pain. Why not

 delete p=0;

i.e., delete should return a reference to its argument. (Perhaps
we need (delete p)=0; ?)

--
;----------------------------------------------------------------------
        JOHN (MAX) SKALLER,         maxtal@extro.ucc.su.oz.au
 Maxtal Pty Ltd, 6 MacKay St ASHFIELD, NSW 2131, AUSTRALIA
;--------------- SCIENTIFIC AND ENGINEERING SOFTWARE ------------------




Author: jamshid@ut-emx.uucp (Jamshid Afshar)
Date: 21 Oct 92 18:49:36 GMT
Raw View
In article <WARSAW.92Oct15103415@anthem.nlm.nih.gov> warsaw@nlm.nih.gov (Barry A. Warsaw) writes:
>On a related topic, I've always wondered why operator delete was
>required to take a void* as its first arg and not void*&. I
>understand why a deleted ptr is not nulled by default (and agree with
>the reasoning), but I should be able to add that functionality to my
>classes if I'm willing to incur any associated costs.  With
>[void operator delete(void*)], I can't even do it.

Remember, just because you can assign any pointer value to a 'void*',
that does not mean you can initialize a 'void*&' with any pointer.
This might be easier to see as:
 int* ip = ...;
 void* p = ip;     // okay
 void*& vp1 = p;   // okay
 void*& vp2 = ip;  // error
 void** vpp = &ip; // error
An 'int*' might not be the same size or have the same representation
as a 'void*', so the last two are errors.

I think explicitly setting the pointer to 0 after a delete (when
necessary) would be the best approach and would cause the least
confusion and surprise for other C++ programmers.

Jamshid Afshar
jamshid@emx.utexas.edu




Author: dak@tabaqui.informatik.rwth-aachen.de (David Kastrup)
Date: 9 Oct 92 17:53:08 GMT
Raw View
Kayvon Z. Sadeghi <KSADEGH@auvm.american.edu> writes:

>1- how do you change the value of a pointer passed to a subroutine? It seems
>to me like you can do almost anything with it except changing the value
>to NULL. This is what I mean:

>pass(struct something *p){
>         p=NULL;
>}

>main(){
>      struct something *apointer;

>      pass(apointer);
>}

You passed the pointer as a value (so it is copied to a local variable
of pass). Changing that pointer in pass has no effect on the original
one. changing the referenced items has, of course. In traditional C,
you would have had to pass a pointer to your pointer, that is:
pass(struct something **p){
 *p =NULL;
}

main(){
 struct something * apointer;
 pass(&apointer);
}

In C++, you can use the original code if you know what you are doing
(of course also if you do not:-') by declaring
void pass(struct something * &p)

C++ will know that it has to pass a reference, then.