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

'delete &Buffer' is legal ?

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
--------------------------------------------------------
And I don't wanna miss a thing
Jul 22 '05 #1
4 1503

"Timothy Madden" <ba****@rmv.spam.home.ro> wrote in message
news:2s*************@uni-berlin.de...
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 -
Jul 22 '05 #2
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
Jul 22 '05 #3
"Timothy Madden" <ba****@rmv.spam.home.ro> schrieb im Newsbeitrag
news:2s*************@uni-berlin.de...


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
Jul 22 '05 #4

"Risto Lankinen" <rl******@hotmail.com> wrote in message
news:Yv*******************@news1.nokia.com...

"Timothy Madden" <ba****@rmv.spam.home.ro> wrote in message
news:2s*************@uni-berlin.de...
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.

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
---------------------------------------------------
And I don't wanna miss a thing
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: John Smith | last post by:
Hello all, I don't know c# that well and thus need urget help. I have a page that displays files from an access database that has a button to be delet file from database. I need a confirmation...
10
by: Alex Vinokur | last post by:
What is wrong with small_buffer in program below? I/O getline doesn't read data from file into small (relative to file line size) buffer. ====== foo.cpp ====== #include <cassert> #include...
2
by: derek.google | last post by:
I have an application that's crashing because of an alignment problem, and this is the smallest program that demonstrates what's happening: int main() { struct Message { unsigned short size;...
7
by: AB | last post by:
Hi all, A thought crossed my mind.... if I allocate memory for an array at runtime using.... int* arr = new int ; what happens when I then de-allocate memory using
11
by: asimorio | last post by:
Hi all, If I don't new up a pointer, can I delete it? see code below: void A::foo() { char* buffer; delete buffer; return; }
4
by: asimorio | last post by:
All, Well, now i modify a bit my code: Will it make sense? void A::Foo(void* a) { char* buffer = (char*) a; delete buffer; return; }
1
by: JustSomeGuy | last post by:
My question is on the delete syntax If I allocate a buffer as follows: unsigned char * buffer = new unsigned char; Then I was told I needed to delete the buffer as: delete buffer;
2
by: tom | last post by:
how to copy struct to buffer? char * buffer; buffer=(char *)malloc(5*20); Data_O tt; strcpy(tt.Name1, "John ");
5
by: Sean | last post by:
Hi, I have a variable declared as the following: char * buf; I then get a size of a file and allocate memory and point the buf to it. Here is where I am having a problem. I want to know how I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.