By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,851 Members | 2,104 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,851 IT Pros & Developers. It's quick & easy.

References and destruction

P: n/a
Assuming I have an object containing a reference to something (which is
passed along with the object's constructor), does the automatic default
destructor take care of destroying the reference's object, or do I have
to write my own explicit destructor? If not, does the default
destructor work for pointers (assuming they're non-NULL)?
Jul 2 '06 #1
Share this Question
Share on Google+
18 Replies


P: n/a
Ulrich Hobelmann posted:
Assuming I have an object containing a reference to something (which is
passed along with the object's constructor), does the automatic default
destructor take care of destroying the reference's object
No.
or do I have
to write my own explicit destructor?

Yes.

#include <new>

class Arb {
public:

int *p;

int &r;
Arb() : p( new(std::nothrow) int[64] ),
r( *new(std::nothrow) int[64] ) {}

~Arb()
{
/* Both of these delete's are necessary: */

delete [] p;

delete [] &r;
}
};
--

Frederick Gotham
Jul 2 '06 #2

P: n/a
Frederick Gotham wrote:
>or do I have
to write my own explicit destructor?


Yes.
Ok, thanks :)
Jul 2 '06 #3

P: n/a
Frederick Gotham wrote:
~Arb()
{
/* Both of these delete's are necessary: */

delete [] p;

delete [] &r;
}
};
Hm, does it work with non-arrays too?

When I try to delete my reference, the compiler says "class argument
given to delete; expected pointer".

If it really doesn't work for references, I'll have to go with pointers,
after all (even though refs are much nicer, and my pointers are const
and non-null anyway)...
Jul 2 '06 #4

P: n/a
Ulrich Hobelmann posted:
Frederick Gotham wrote:
> ~Arb()
{
/* Both of these delete's are necessary: */

delete [] p;

delete [] &r;
}
};

Hm, does it work with non-arrays too?

"p" is a pointer variable. It stores an address. The address stored
within it is the address of the first element of a dynamically allocated
array.

"r" is a reference. It refers to the first element of a dynamically
allocated array.

The operator, "delete []", seeks as an operand the address of the first
element of a dynamically allocated array. Therefore:

delete [] p;

delete [] &r; /* Note the address-of operator */
--

Frederick Gotham
Jul 2 '06 #5

P: n/a
Frederick Gotham schrieb:
#include <new>
Why do you, for a "minimal" example, use include <newand use std::nothrow?
class Arb {
public:

int *p;

int &r;
Arb() : p( new(std::nothrow) int[64] ),
r( *new(std::nothrow) int[64] ) {}
A reference "pointing" to a non-object? Doesn't the standard guarantee,
that a reference is always bound to an object?
~Arb()
{
/* Both of these delete's are necessary: */

delete [] p;

delete [] &r;
}
};

Thomas
Jul 3 '06 #6

P: n/a
Thomas J. Gritzan posted:
Frederick Gotham schrieb:
>#include <new>

Why do you, for a "minimal" example, use include <newand use
std::nothrow?

Q. Why did I include <new>?
A. To use a feature declared in that header.

Q. Why did I use nothrow?
A. So that a dynamic allocation failure wouldn't result in an exception
being thrown.

A reference "pointing" to a non-object?

Before you question my code, may I suggest that you know what you're
talking about?

The operator "new []" evaluates to a pointer to the first element of the
dynamically allocated array. Dereference that pointer and you have the
first element of the array. There is no "non-object" to speak of.

Stare at this for twenty minutes until it sinks in:

#include <iostream>

int main()
{
int i = 0;

int *p = &i;

int &r = *p;

r = 666;

std::cout << i << '\n';
}

Doesn't the standard guarantee, that a reference is always bound to an
object?

The Standard stipulates that a reference must refer to a valid object;
thus the following code is broken:

int main()
{
int *p = 0;

int &r = *p; /* Forbidden */
}
As is the following:
int main()
{
int array[8];

int *p = array + 8; /* No problem */

int &r = *p; /* Forbidden */
}
--

Frederick Gotham
Jul 3 '06 #7

P: n/a
Frederick Gotham schrieb:
Q. Why did I use nothrow?
A. So that a dynamic allocation failure wouldn't result in an exception
being thrown.
Instead, it returns a null-pointer.
Before you question my code, may I suggest that you know what you're
talking about?
I do.
The operator "new []" evaluates to a pointer to the first element of the
dynamically allocated array. Dereference that pointer and you have the
first element of the array. There is no "non-object" to speak of.
Not my point.

[snip]
>Doesn't the standard guarantee, that a reference is always bound to an
object?


The Standard stipulates that a reference must refer to a valid object;
thus the following code is broken:

int main()
{
int *p = 0;

int &r = *p; /* Forbidden */
}
Exactly.

Q1: What does new when an allocation failure happens?
Q2: What does new(std::nothrow) in this case?
Q3: What happens, if you do

int &r = new(std::nothrow) int;

in case of an allocation failure?

Thomas
Jul 3 '06 #8

P: n/a
Thomas J. Gritzan schrieb:
Q1: What does new when an allocation failure happens?
Q2: What does new(std::nothrow) in this case?
Q3: What happens, if you do

int &r = new(std::nothrow) int;
Should be:

int &r = *new(std::nothrow) int;
in case of an allocation failure?

Thomas
--
Thomas
Jul 3 '06 #9

P: n/a
Hi,
I see all heated arguments over here. Don't know why? Is this an
information exchange group or is this meant for throwing stones on one
another?

-- Murali Krishna

Jul 3 '06 #10

P: n/a

"Frederick Gotham" <fg*******@SPAM.comwrote in message
news:Vy*******************@news.indigo.ie...
Ulrich Hobelmann posted:
>Assuming I have an object containing a reference to something (which is
passed along with the object's constructor), does the automatic default
destructor take care of destroying the reference's object

No.
True, but that's because you don't WANT to destroy a reference. It refers
to an object whose lifetime is managed elsewhere.
>
>or do I have
to write my own explicit destructor?


Yes.
That's not true (except in your example, where you use new to create a new
object for the reference to refer to).

A reference does not need to be destroyed, and there is no direct mechanism
for doing so. And even if you could, that would usually result in multiple
destruction of an object. A reference refers to an existing object, and the
lifetime of that object is usually managed elsewhere. So destroying the
object via its reference would leave whatever was managing the lifetime of
the object in an invalid state.

The example given just happens to be using a reference member to point to
some memory which is newed and must therefore be deleted somewhere (and the
only thing refering to that memory happens to be a reference variable).
It's a contrived example, and isn't really germaine to the question.

Do references need to be deleted? The answer is no.

-Howard



Jul 3 '06 #11

P: n/a
Howard posted:

Do references need to be deleted? The answer is no.

I gathered from the original post in the thread that the OP was referring
to references which refer to dynamically allocated objects.
--

Frederick Gotham
Jul 3 '06 #12

P: n/a

"Frederick Gotham" <fg*******@SPAM.comwrote in message
news:1D*******************@news.indigo.ie...
Howard posted:

>Do references need to be deleted? The answer is no.


I gathered from the original post in the thread that the OP was referring
to references which refer to dynamically allocated objects.
Understood. But in most cases, such objects are dynamically allocated and
assigned to a pointer somewhere, and it is that pointer which needs to have
delete called on it, not any reference variables which may later refer to
it. Just wanted the OP to understand that.

-Howard
Jul 3 '06 #13

P: n/a
Howard wrote:
"Frederick Gotham" <fg*******@SPAM.comwrote in message
news:Vy*******************@news.indigo.ie...
>Ulrich Hobelmann posted:
>>Assuming I have an object containing a reference to something (which is
passed along with the object's constructor), does the automatic default
destructor take care of destroying the reference's object
No.

True, but that's because you don't WANT to destroy a reference. It refers
to an object whose lifetime is managed elsewhere.
Oh, that's the purpose? Hmm, well I'm building a recursive data
structure. Does that mean my only option is pointers? (maybe I've
gotten the wrong impression, that pointers aren't much used anymore in
C++, and are mostly a C legacy)
A reference does not need to be destroyed, and there is no direct mechanism
for doing so. And even if you could, that would usually result in multiple
Well, I pass in a newly created object which needs to be destroyed when
the containing object is. It's basically like a tree.
Jul 3 '06 #14

P: n/a
Howard wrote:
"Frederick Gotham" <fg*******@SPAM.comwrote in message
news:1D*******************@news.indigo.ie...
>Howard posted:

>>Do references need to be deleted? The answer is no.

I gathered from the original post in the thread that the OP was referring
to references which refer to dynamically allocated objects.

Understood. But in most cases, such objects are dynamically allocated and
assigned to a pointer somewhere, and it is that pointer which needs to have
delete called on it, not any reference variables which may later refer to
it. Just wanted the OP to understand that.
Oh sure, understood :)
Jul 3 '06 #15

P: n/a

"Ulrich Hobelmann" <u.*********@web.dewrote in message
news:4g*************@individual.net...
Howard wrote:
>"Frederick Gotham" <fg*******@SPAM.comwrote in message
news:Vy*******************@news.indigo.ie...
>>Ulrich Hobelmann posted:

Assuming I have an object containing a reference to something (which is
passed along with the object's constructor), does the automatic default
destructor take care of destroying the reference's object
No.

True, but that's because you don't WANT to destroy a reference. It
refers to an object whose lifetime is managed elsewhere.

Oh, that's the purpose? Hmm, well I'm building a recursive data
structure. Does that mean my only option is pointers?
I can't tell without seeing your code, or at least something describing the
structure in question.

References may be fine. Pointers may be fine. It all depends on details we
don't see here.
(maybe I've gotten the wrong impression, that pointers aren't much used
anymore in C++, and are mostly a C legacy)
Pointers are used quite often in C++. It's often preferable to avoid them
(in favor of automatic objects) if you don't need them, but it's also true
that you often need them! :-)
>
>A reference does not need to be destroyed, and there is no direct
mechanism for doing so. And even if you could, that would usually result
in multiple

Well, I pass in a newly created object which needs to be destroyed when
the containing object is. It's basically like a tree.
Since you didn't post any code, I can't tell exactly what you mean. It
_may_ be that what you want is a pointer member, not a reference member. No
way to tell without seeing your code.

But if you _do_ use pointers, you need to be sure that each
dynamically-allocated object is destroyed exactly once.

-Howard

Jul 3 '06 #16

P: n/a
Howard wrote:
I can't tell without seeing your code, or at least something describing the
structure in question.

References may be fine. Pointers may be fine. It all depends on details we
don't see here.
As I said, base class + several subclasses. All references are mostly
to the base class, so I have to use pointers or refs, no direct objects,
as the size varies.

It's mostly a tree, with the constructor being called with the node's
child nodes (of the base class)...

new SubNodeA(new SubNodeB(), bla) and stuff like that.
>(maybe I've gotten the wrong impression, that pointers aren't much used
anymore in C++, and are mostly a C legacy)

Pointers are used quite often in C++. It's often preferable to avoid them
(in favor of automatic objects) if you don't need them, but it's also true
that you often need them! :-)
Ok, then I'm fine.
Since you didn't post any code, I can't tell exactly what you mean. It
_may_ be that what you want is a pointer member, not a reference member. No
way to tell without seeing your code.
I used pointers in the C version, but I thought refs might be nicer in
C++, because that implies they're immutable and non-NULL (which they are).
But if you _do_ use pointers, you need to be sure that each
dynamically-allocated object is destroyed exactly once.
Yep, it's a tree, so no problems there. References to external objects
can use refcounting; everything is cycle-free.
Jul 3 '06 #17

P: n/a
Ulrich Hobelmann wrote:
>True, but that's because you don't WANT to destroy a reference. It
refers to an object whose lifetime is managed elsewhere.
Oh, that's the purpose? Hmm, well I'm building a recursive data
structure. Does that mean my only option is pointers? (maybe I've
gotten the wrong impression, that pointers aren't much used anymore in
C++, and are mostly a C legacy)
The thing being destroyed is not a pointer, is an object. The pointer just
points, as is his habitual work, to the object you want to destroy. If you
maintains a reference to the dynamically created object instead of a
pointer to it, you can provide a pointer just by using &, there is no need
to add another syntax or semantic to use delete with references.

--
Salu2

Inviato da X-Privat.Org - Registrazione gratuita http://www.x-privat.org/join.php
Jul 3 '06 #18

P: n/a

Thomas J. Gritzan wrote:
Thomas J. Gritzan schrieb:
Q1: What does new when an allocation failure happens?
Q2: What does new(std::nothrow) in this case?
Q3: What happens, if you do
int &r = *new(std::nothrow) int;
in case of an allocation failure?
you will be judged by commiting a fatal sin

exceptions is the only way to avoid dangling references. this is one of
most imprudent code snippets i've ever seen

Jul 7 '06 #19

This discussion thread is closed

Replies have been disabled for this discussion.