'delete &Buffer' is legal ?

R

Risto Lankinen

Timothy Madden said:
Hello

Is my program legal and well formed:

class Data;
class SMax
{
class Data &Buffer;
public:
SMax(class Data &buffer)
:Buffer(buffer) { }

~SMax()
{ delete &Buffer; }
}

?
That is, can I delete the address within a reference ?

Purely technically, yes.

However, in practice you are placing a lot of constraints
to the user of your class. For instance, whoever instantiates
class SMax will have to provide a [reference to a] deletable
Data object, requiring all buffers to be dynamically allocated.
Also, if the caller is responsible for the lifetime management
of the buffer, you run the risk that a buffer might be deleted
while some SMax object still holds a refeence to it.

This is a lot of responsibility to be placed on the user of
your class, so although its design is technically correct, it
could be improved to be more robust and user friendly.

Improvement ideas (from most to least recommended):
- Let the class itself both create and delete the buffer
- Make class Data reference counted instead
- Let the caller both create and delete the buffer

Cheers!

- Risto -
 
T

Timothy Madden

Hello

Is my program legal and well formed:

class Data;
class SMax
{
class Data &Buffer;
public:
SMax(class Data &buffer)
:Buffer(buffer) { }

~SMax()
{ delete &Buffer; }
}

?
That is, can I delete the address within a reference ?

Thank you
Timothy Madden
Romania
 
J

JKop

Timothy Madden posted:
Hello

Is my program legal and well formed:

class Data;
class SMax
{
class Data &Buffer;
public:
SMax(class Data &buffer)
:Buffer(buffer) { }

~SMax()
{ delete &Buffer; }
}


That's not a program. The following is a program and is well formed
(assuming a definition is given for "Data"). (And I won't pass judgement on
your coding style. . .):


class Data;
class SMax
{
class Data &Buffer;
public:
SMax(class Data &buffer)
:Buffer(buffer) { }

~SMax()
{ delete &Buffer; }
}


int main()
{
{
Data &blah = *new Data;

SMax(blah); //no problems
}

{
Data blah;
SMax(blah); //Problem
}
}


-JKop
 
M

Michael Kurz

Timothy Madden said:
class Data;
class SMax
{
class Data &Buffer;
public:
SMax(class Data &buffer)
:Buffer(buffer) { }

~SMax()
{ delete &Buffer; }
}

?
That is, can I delete the address within a reference ?

Yes, but IMHO this is not typical, so probably many users of your class will
not know which kind of object to provide dynamically allocated / or not and
if dynamically allocated who is responsible for freeing.

So I would suggest to use a pointer instead. Even better would be to use
std::auto_ptr<Data> instead so it will be clear that your class will be
responsible for the buffer after it has been transfered to it without the
need
to rely on documentation.

Regards
Michael
 
T

Timothy Madden

Risto Lankinen said:
Purely technically, yes.

Improvement ideas (from most to least recommended):
- Let the class itself both create and delete the buffer
- Make class Data reference counted instead
- Let the caller both create and delete the buffer
I want the user to allocate the buffer, test the resulted pointer and if it
is ok
then pass responsibility for it to a new SMax object. Otherwise the user can
immediately
throw something since it faces insufficient memory.

I delete the buffer in the destructor because this is the way exception
handling is meant to be used in a program. If I had let the user delete the
buffer than the user should delete it every time it wants to throw an
exception. With my aproach the user can just throw, on matter how many
buffers have been allocated; they are all in control of the SMax destructor

Timothy Madden
Romania
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,176
Messages
2,570,950
Members
47,503
Latest member
supremedee

Latest Threads

Top