473,796 Members | 2,595 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

'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 1540

"Timothy Madden" <ba****@rmv.spa m.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.spa m.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<D ata> 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******@hotma il.com> wrote in message
news:Yv******** ***********@new s1.nokia.com...

"Timothy Madden" <ba****@rmv.spa m.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
6976
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 box to make sure the user wants to delet the file. Here is my code: private string GetFilesFromAccessDb() {
10
3825
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 <iostream> #include <fstream> using namespace std; void read_using_io_getline ()
2
3022
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; }; const int START_INDEX = 1; char* buffer = new char; Message* msg = (Message*)&buffer;
7
7029
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
2370
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
2233
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
1559
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
9688
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
6344
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 can read the buf in chunks... so here is a description of what i am looking for.:
0
9679
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10453
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10223
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10003
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9050
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7546
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5573
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4115
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.