473,396 Members | 2,108 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,396 software developers and data experts.

References and destruction

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
18 2547
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
Frederick Gotham wrote:
>or do I have
to write my own explicit destructor?


Yes.
Ok, thanks :)
Jul 2 '06 #3
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
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
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
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
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
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
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

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

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

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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Gandalf | last post by:
Hello (it's the newbie again). If I have a class class Foo{ public: Foo(){cout<<"Making"<<endl;} ~Foo(){cout<<"Destroying"<<endl;} }; void func(Foo x){}
14
by: Gama Franco | last post by:
Hi, I'm designing an interface for a shared library, and I would like to know if there is a standard about how to return an object to the user. I will use exceptions to report errors, so there...
0
by: Stephan Keil | last post by:
Hi all, consider a class class X { static X& OneX() { static X sgl1; return sgl1; } static X& TheX() { static X sgl2(OneX()); return sgl2; } }; and two source files with global variables
2
by: Oliver Sturm | last post by:
Say I have a data class: class Data { public List<SubData> SubDataList1 { get ... set ... } public List<Subdata> SubDataList2 { get ... set ... } } It's possible that a specific SubData...
31
by: anongroupaccount | last post by:
I have an ABC with a protected member that is a reference to an object of an ABC type: class ABC { public: virtual ~ABC() = 0; protected: AnotherABC& _member; }
9
by: plahey | last post by:
I have been dabbling in Python for a while now. One of the things that really appeals to me is that I can seem to be able to use C++-style RAII idioms to deal with resource management issues. ...
6
by: Pablo | last post by:
Hello, I am writing a windows application using C++ and BorlandBuilder 6 compiler. It is an event driven program and I need to create objects of some classes written by me. One of the classes...
2
by: Dennis Jones | last post by:
Hello, I have a class that will eventually look something like this: class TTableHolder { private: boost::scoped_ptr<TSessionFSession; boost::shared_ptr<TTableFTable;
11
by: rayreeves | last post by:
How do I declare an array of references to the elements of an array of values? Ray
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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.